首页 > 其他分享 >Typescript类型体操 - RemoveIndexSignature

Typescript类型体操 - RemoveIndexSignature

时间:2022-09-21 00:55:38浏览次数:104  
标签:Typescript string symbol number RemoveIndexSignature keyof 体操 type

题目

中文

实现 RemoveIndexSignature<T>, 将索引字段从对象中排除掉.

示例:

type Foo = {
    [key: string]: any;
    foo(): void;
};
type A = RemoveIndexSignature<Foo>; // expected { foo(): void }

English

Implement RemoveIndexSignature<T> , exclude the index signature from object types.

For example:

type Foo = {
    [key: string]: any;
    foo(): void;
};
type A = RemoveIndexSignature<Foo>; // expected { foo(): void }

答案

type RemoveIndexSignature<T extends object> = {
    [P in keyof T as string extends P
        ? never
        : number extends P
        ? never
        : symbol extends P
        ? never
        : P]: T[P];
};

在线演示

解析

关键在于理解当对一个包含索引签名(Index Signature)的对象类型执行keyof会返回什么, 看下面几个例子:

type Foo = keyof { [key: string]: any }; // Foo 的类型是 string | number
type Bar = keyof { [key: symbol]: any }; // Bar 的类型是 symbol
type FooBar = keyof { [key: string | number | symbol]: any }; // FooBar 的类型是 string | number | symbol
type FooFoo = keyof { name: string; age: string }; // FooFoo的类型是 'name' | 'age'

keyof某个对象时, 对象中普通的字段会变成字面量类型, 对应上面例子中的FooFoo, 而索引签名会变成 string number symbol 这些类型组合出来的联合类型, 知道了这点就可以判断是否为索引签名了

type isIndexSignature<T> = string extends T
    ? true
    : number extends T
    ? true
    : symbol extends T
    ? true
    : false;

标签:Typescript,string,symbol,number,RemoveIndexSignature,keyof,体操,type
From: https://www.cnblogs.com/laggage/p/type-challenge-remove-index-signature.html

相关文章

  • Typescript类型体操 - PickByType
    题目中文找出T中类型为U的属性示例:typeOnlyBoolean=PickByType<{name:string;count:number;isReadonly:boolean;isE......
  • Typescript类型体操 - MinusOne
    题目中文给定一个正整数作为类型的参数,要求返回的类型是该数字减1。例如:typeZero=MinusOne<1>;//0typeFiftyFour=MinusOne<55>;//54EnglishGivenanu......
  • typescript-变量
    1.变量赋值了类型就不能赋值其他类型1leta:number;2letb:string;3a=10;45//不可以6//a="assdf";7b="123"2.如果变量的声明和赋值是同时......
  • Typescript类型体操 - PercentageParser
    题目中文实现类型PercentageParser。根据规则/^(\+|\-)?(\d*)?(\%)?$/匹配类型T。匹配的结果由三部分组成,分别是:[正负号,数字,单位],如果没有匹配,则默认是空字符串......
  • Typescript类型体操 - DropChar
    题目中文从字符串中剔除指定字符。例如:typeButterfly=DropChar<'butterfly!',''>;//'butterfly!'EnglishDropaspecifiedcharfromastring.......
  • Typescript类型体操 - ReplaceKeys
    题目中文实现一个ReplaceKeys类型,这个类型可以替换联合类型中指定属性的类型,如果联合类型中的某个类型没有这个属性,那就跳过;ReplaceKeys接受3个泛型参数.例如......
  • 在 TypeScript 中指定 event.target 的类型
    在TypeScript中指定event.target的类型让我们解决使用ClassList和dataset等属性时出现的错误!案件由来本文出现的所有错误都是基于eslint产生的错误。我想通过......
  • [Typescript] 18. Medium - Deep Readonly
    Implementageneric DeepReadonly<T> whichmakeeveryparameterofanobject-anditssub-objectsrecursively-readonly.Youcanassumethatweareonlydea......
  • [Typescript] 17. Medium - Readonly 2
    Implementageneric MyReadonly2<T,K> whichtakestwotypeargument T and K.K specifythesetofpropertiesof T thatshouldsettoReadonly.When K......
  • Typescript类型体操 - kebab case
    题目中文将camelCase或PascalCase的字符串转换为kebab-case的风格示例:typeFooBarBaz=KebabCase<'FooBarBaz'>;constfoobarbaz:FooBarBaz='foo-bar-baz'......