Typescript类型定义

代码示例

6
0

typescript中的自定义类型

// simple type
type Websites = 'www.google.com' | 'reddit.com';
let mySite: Websites = 'www.google.com' //pass
//or
let mySite: Website = 'www.yahoo.com' //error
// the above line will show error because Website type will only accept 2 strings either 'www.google.com' or 'reddit.com'.
// another example. 
type Details = { id: number, name: string, age: number };
let student: Details = { id: 803, name: 'Max', age: 13 }; // pass
//or 
let student: Details = { id: 803, name: 'Max', age: 13, address: 'Delhi' } // error
// the above line will show error because 'address' property is not assignable for Details type variables.
//or
let student: Details = { id: 803, name: 'Max', age: '13' }; // error
// the above line will show error because string value can't be assign to the age value, only numbers.


3
0

typescript函数类型

// define your parameter's type inside the parenthesis
// define your return type after the parenthesis

function sayHello(name: string): string  {
  console.log(`Hello, ${name}`!);
}

sayHello('Bob'); // Hello, Bob!
2
0

typescript函数类型

interface Date {
  toString(): string;
  setTime(time: number): number;
  // ...
}
2
0

typescript类型定义

// 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)
1
0

在typescript中键入

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

类似页面

带有示例的类似页面

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
English
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................
Балгарскі
..................................................................................................................
Íslensk
..................................................................................................................