Skip to content

学习笔记 #2

Open
Open
@coderhyy

Description

@coderhyy

as 的用法

在映射时经常会用到 as 关键词,像下面代码一样

type MappedKey<T> = {
  [K in keyof T as K extends xxx ? never : K]: T[K];
};

这里 as 后面的语句使用来做筛选的

遍历时得到的 K 需要做一些特殊的处理时,比如给 K 加上前缀或后缀,筛选符合某种条件的 K,用 as 连接条件语句

as 是 typescript 4.1 功能,文档链接: Key Remapping in Mapped Type

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions