Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

how to define types in typescript

type Person = {
  name: string;
  age: number;
};

const person: Person {
	named: 'Rex'
  	age: 23
}
 
function greet(person: Person) {
  return "Hello " + person.name;
}
Comment

define typescript types

let name: string; // stores text
let age: number; // stores numbers
let isMale: boolean; // stores a true or false values
let typeAny: any; // stores any type of value
let typeNull: null = null; // can only store a null value
let typeUndefined: undefined = undefined; // can only store undefined value

// these are the basic types of variables in TypeScript
// and of course you can change let to const
Comment

typescript type definition

// cannot use object for type defination because this is not recommended
// use Record<string, any> this same with object

const name: string = "john doe"
const age: number = 30

const days1: string[] = ["sunday","monday","thuesday","wenesday"]
const numb1: number[] = [1,2,3,4,5]

const days2: Array<string> = ["sunday","monday","thuesday","wenesday"]
const numb2: Array<number> = [1,2,3,4,5]

const person: Record<string, any> = {
	name: "john doe",
	age: 30
}

async function name(): Promise<string> {
	return "john doe"
}

name().then(console.log)

async function str(): Promise<string[]> {
	return ["sunday","monday","thuesday","wenesday"]
}

str().then(console.log)

async function int(): Promise<int[]> {
	return [1,2,3,4,5]
}

int().then(console.log)

async function objectValue(): Promise<Record<string, any>> {
	const person: Record<string, any> = {
	 name: "john doe",
	 age: 30
	}
  return person
}

objectValue().then(console.log)

async function objectValueMulti(): Promise<Record<string, any>[]> {
	const person: Record<string, any>[] = [{
	 name: "john doe",
	 age: 30
	},{
	 name: "jane doe",
	 age: 30
	}]
  return person
}

objectValueMulti().then(console.log)
Comment

make a type in typescript

type Props = {
  item: CartItemType;
  addToCart: (clickedItem: CartItemType) => void;
  removeFromCart: (id: number) => void;
};
Comment

type of typescript

// Prints "string"
console.log(typeof "Hello world");
Comment

typing in typescript

var name: string = "Anna";
let notes: (number | string)[] = ["Get Food", 23, "Call the previous number when betting"];
Comment

how to define types in typescript

interface Person {
  name: string;
  age: number;
}

const person:Person {
	name: "Rex",
    age: 20
}
 
function greet(person: Person) {
  return "Hello " + person.name;
}
Comment

type in typescript


    function identity<Type>(arg: Type): Type {
      return arg;
    }


let fun = identity<string>("hello world");
console.log(fun);   
/*think of this as you can specify the type later*/
/*Directly specifying version of the above would be something like
    function identity(arg: string): string {
      return arg;
    }


let fun = identity("hello world");
console.log(fun);   

*/
Comment

TS define this type

grunt.registerMultiTask('clean', function(this: SomeType) {
    //...
});
Comment

PREVIOUS NEXT
Code Example
Typescript :: typescript cheatsheet 
Typescript :: ng2003 
Typescript :: slice string into segments of 2 characters 
Typescript :: minuts bwtewwn two date laravel 
Typescript :: typescript vite static assets 
Typescript :: Please fill in the missing parts of the code to print "I love C++" on the screen. 
Typescript :: A HTML5 fullscreen plugin for Leaflet. 
Typescript :: components of loadrunner 
Typescript :: how to permit only a few values in dbms 
Typescript :: terminal update file metadata 
Typescript :: increment elements in array typescript 
Typescript :: vscode tsc.ps1 command not loaded 
Typescript :: modifying 2d lists python 
Typescript :: flutter animate size change 
Typescript :: Carbohydrates and fats both 
Typescript :: export email accounts for a domain cpanel 
Typescript :: typescript custom number no greater than x 
Typescript :: url prod 
Typescript :: add padding between elements of lazyrow jetpack compose 
Typescript :: enum to number typescript 
Typescript :: number of increments and decrements to make array sorted 
Typescript :: How can I create an array with a range of decimal increments in SwiftUI ? 
Typescript :: how to i count objects available in salesforce organization 
Typescript :: Error detected in pubspec.yaml: No file or variants found for asset: assets/imgs. 
Typescript :: pptxgenjs bullet 
Typescript :: recharts direction 
Typescript :: config all requests to one page nginx 
Typescript :: add custom text after title of products on achive page 
Typescript :: can check constraints reference other tables 
Typescript :: Passing a generic function in as a callback in Typescript 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =