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

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

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

typescrpt create a type

type numberOrString = number | string
type numberType = 1|2|3|4|5|6|7|8
type letterType = 'a'|'b'|'c'|'d'|'e'|'f'|'g'|'h'|'i'
// https://www.typescriptlang.org/docs/handbook/2/types-from-types.html
Comment

TS define this type

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

PREVIOUS NEXT
Code Example
Typescript :: How can I call a method every x seconds? 
Typescript :: startswith multiple arguments python 
Typescript :: typescript variable 
Typescript :: Error: "prettier/@typescript-eslint" has been merged into "prettier" in eslint-config-prettier 8.0.0 
Typescript :: paragraph dots after 2 lines css 
Typescript :: typescript string concatenation best practice 
Typescript :: readonly in typescript 
Typescript :: import ts in html 
Typescript :: compare two lists and find at least one equal python 
Typescript :: typescript interface to http params 
Typescript :: pass command line arguments with spaces cmd 
Typescript :: show the current time realtime in vue 
Typescript :: angular images 
Typescript :: what is hello world in typescript 
Typescript :: cacerts default password 
Typescript :: Include Type TypeScript 
Typescript :: world-times-newspaper-magazine-style-ghost-blog-theme 
Typescript :: why do we write unit tests in programming 
Typescript :: dynamic key interface typescript 
Typescript :: Angular Compiler Options to enable AOT compilation 
Typescript :: requests get with sign in 
Typescript :: how to get an object from array of objects in java 
Typescript :: ModuleNotFoundError brython 
Typescript :: Destructuring props in styled-components 
Typescript :: piechart am4charts legend with actual values 
Typescript :: sum the digits in c 
Typescript :: how did mississauga get its name 
Typescript :: Which of the following statements will compile correctly? 
Typescript :: how to write elements of a list as a string with a comma between elements in python 
Typescript :: google sheets formula pull last columns 
ADD CONTENT
Topic
Content
Source link
Name
2+4 =