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 variable 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

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 :: makestyles material ui typescript 
Typescript :: excel check if value exists in range 
Typescript :: ts compile command 
Typescript :: material dialog disable close 
Typescript :: type definition method typescript 
Typescript :: Mongodb count based on array of ids 
Typescript :: nestjs graphql schema description 
Typescript :: unknown type in typescript 
Typescript :: ERROR in The Angular Compiler requires TypeScript =3.4.0 and <3.6.0 but 4.1.5 was found instead. 
Typescript :: ng2003 
Typescript :: fetch tweets 
Typescript :: typescript ingerit 
Typescript :: nest js crons intialization 
Typescript :: No provider for ChildrenOutletContexts! 
Typescript :: increment elements in array typescript 
Typescript :: Actual instructions in flowcharts are represented in __________ 
Typescript :: react table typing errors, filters, sorting and paging 
Typescript :: conditional statements in ti-82 
Typescript :: Convert the array of objects to object iterable 
Typescript :: gang beasts türkiye discord 
Typescript :: how to gray out the unused imports in vscode 
Typescript :: java concepts mcq 
Typescript :: what do you need local sccripts for 
Typescript :: ts types passing functions 
Typescript :: powershell copy contents of keyvault to another keyvault 
Typescript :: nestjs called once method 
Typescript :: js Validating promises 
Typescript :: recharts direction 
Typescript :: carousel not moving unless reload the page 
Typescript :: get alphabets and space only from xml file in android studio 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =