JavaScript/typescript/type:修订间差异
来自康健生活
创建页面,内容为“== 引言 == == 示例 == <syntaxhighlight lang="javascript"> type User = { name: string age: number }; type SetUser = (name: string, age: number): void; </syntax…” |
|||
| (未显示同一用户的1个中间版本) | |||
| 第35行: | 第35行: | ||
age: number; | age: number; | ||
}</syntaxhighlight> | }</syntaxhighlight> | ||
== 声明 == | |||
声明基本类型别名,联合类型,元组等类型。 | |||
<syntaxhighlight lang="javascript"> | |||
// 基本类型别名 | |||
type Name = string | |||
// 联合类型 | |||
interface Dog { | |||
wong(); | |||
} | |||
interface Cat { | |||
miao(); | |||
} | |||
type Pet = Dog | Cat | |||
// 具体定义数组每个位置的类型 | |||
type PetList = [Dog, Pet]</syntaxhighlight> | |||
=== typeof === | |||
使用 typeof 获取变量的类型。 | |||
<syntaxhighlight lang="javascript"> | |||
let div = document.createElement('div'); | |||
type B = typeof div</syntaxhighlight> | |||
=== 其他 === | |||
<syntaxhighlight lang="javascript"> | |||
type StringOrNumber = string | number; | |||
type Text = string | { text: string }; | |||
type NameLookup = Dictionary<string, Person>; | |||
type Callback<T> = (data: T) => void; | |||
type Pair<T> = [T, T]; | |||
type Coordinates = Pair<number>; | |||
type Tree<T> = T | { left: Tree<T>, right: Tree<T> };</syntaxhighlight> | |||
{{Unknown}} | |||