TS Type

基本类型别名

type Name = string

联合类型

interface Dog {
    wong();
}
interface Cat {
    miao();
}

type Pet = Dog | Cat

具体定义数组每个位置的类型

type PetList = [Dog, Pet]

获取类型

// 当你想获取一个变量的类型时,使用 typeof

let div = document.createElement('div');
type B = typeof div
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> };
type ParsedType = {
    id: number
}

const parseApiResponse(
    response: Record<string, unknown>
): ParsedType => {
 const convertedResponse = (response as ParsedType) // 通过as转换类型
    // without doing the type cast we would
    // get a type error here
 if (convertedResponse.id >= 0) {
    return convertedResponse
 } else {
    throw Error.new("Invalid response"
 }
}

Notion – The all-in-one workspace for your notes, tasks, wikis, and databases.icon-default.png?t=M0H8https://serious-lose.notion.site/TS-Type-35bd2e6a9185471eb03d41f0a787d329 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值