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

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

PREVIOUS NEXT
Code Example
Typescript :: typescript variables 
Typescript :: typescript import css 
Typescript :: typescript check if object is of type 
Typescript :: simulate click typescript 
Typescript :: typescript one of the array items 
Typescript :: mixpanel for typescript 
Typescript :: typescript parameter function type 
Typescript :: npm typescript package 
Typescript :: angular rxjs 
Typescript :: Lists - Learn C# 
Typescript :: available ports for localhost 
Typescript :: subway restaurants in israel 
Typescript :: whats ruby used for 
Typescript :: shortid typescript 
Typescript :: Global CSS cannot be imported from files other than your Custom <App 
Typescript :: in grunt cannot be loaded because running scripts is disabled on this system 
Typescript :: typescript date before 
Typescript :: O arquivo yarn.ps1 não pode ser carregado porque a execução de scripts foi desabilitada neste sistema 
Typescript :: how to get the table contents from a file in python 
Typescript :: stackoverflow ngbdate angular 
Typescript :: how to read excel spreadsheets in c++ 
Typescript :: fit_transform() takes 2 positional arguments but 3 were given 
Typescript :: ts object field from variable 
Typescript :: ts compile command 
Typescript :: split in angular 8 
Typescript :: aws s3 list objects by size 
Typescript :: how to print brackets characters in c# 
Typescript :: Count by One Variable 
Typescript :: c# ienumerable wrap to know when its compltee 
Typescript :: how to use aspects in spring boot 
ADD CONTENT
Topic
Content
Source link
Name
5+7 =