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

Typescript类型体操 - ReplaceKeys

时间:2022-09-18 19:23:10浏览次数:146  
标签:Typescript name number flag 体操 type string ReplaceKeys

题目

中文

实现一个ReplaceKeys类型, 这个类型可以替换联合类型中指定属性的类型, 如果联合类型中的某个类型没有这个属性, 那就跳过; ReplaceKeys接受 3 个泛型参数.

例如:

type NodeA = {
    type: 'A';
    name: string;
    flag: number;
};
type NodeB = {
    type: 'B';
    id: number;
    flag: number;
};
type NodeC = {
    type: 'C';
    name: string;
    flag: number;
};
type Nodes = NodeA | NodeB | NodeC;
type ReplacedNodes = ReplaceKeys<
    Nodes,
    'name' | 'flag',
    { name: number; flag: string }
>; // {type: 'A', name: number, flag: string} | {type: 'B', id: number, flag: string} | {type: 'C', name: number, flag: string} // would replace name from string to number, replace flag from number to string.
type ReplacedNotExistKeys = ReplaceKeys<Nodes, 'name', { aa: number }>; // {type: 'A', name: never, flag: number} | NodeB | {type: 'C', name: never, flag: number} // would replace name to never

English

Implement a type ReplaceKeys, that replace keys in union types, if some type has not this key, just skip replacing,
A type takes three arguments.

For example:

type NodeA = {
    type: 'A';
    name: string;
    flag: number;
};
type NodeB = {
    type: 'B';
    id: number;
    flag: number;
};
type NodeC = {
    type: 'C';
    name: string;
    flag: number;
};
type Nodes = NodeA | NodeB | NodeC;
type ReplacedNodes = ReplaceKeys<
    Nodes,
    'name' | 'flag',
    { name: number; flag: string }
>; // {type: 'A', name: number, flag: string} | {type: 'B', id: number, flag: string} | {type: 'C', name: number, flag: string} // would replace name from string to number, replace flag from number to string.
type ReplacedNotExistKeys = ReplaceKeys<Nodes, 'name', { aa: number }>; // {type: 'A', name: never, flag: number} | NodeB | {type: 'C', name: never, flag: number} // would replace name to never

答案

type ReplaceKeys<
    U extends object,
    T extends string,
    Y extends object
> = U extends U
    ? {
          [P in keyof U]: P extends T
              ? P extends keyof Y
                  ? Y[P]
                  : never
              : U[P];
      }
    : never;

在线演示

标签:Typescript,name,number,flag,体操,type,string,ReplaceKeys
From: https://www.cnblogs.com/laggage/p/type-challenge-replace-keys.html

相关文章

  • 在 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'......
  • Typescript类型体操 - String to Union
    题目中文实现一个将接收到的String参数转换为一个字母Union的类型。例如typeTest='123';typeResult=StringToUnion<Test>;//expectedtobe"1"|"2"|......
  • Typescript类型体操 - Absolute
    题目中文实现一个接收string,number或bigInt类型参数的Absolute类型,返回一个正数字符串。例如typeTest=-100;typeResult=Absolute<Test>;//expectedtobe"......
  • Typescript类型体操 - Append to object
    题目中文实现一个为接口添加一个新字段的类型。该类型接收三个参数,返回带有新字段的接口类型。例如:typeTest={id:'1'}typeResult=AppendToObject<Test,'va......
  • Typescript类型体操 - Flatten
    题目中文在这个挑战中,你需要写一个接受数组的类型,并且返回扁平化的数组类型。例如:typeflatten=Flatten<[1,2,[3,4],[[[5]]]]>//[1,2,3,4,5]EnglishIn......
  • Typescript类型体操 - Length of String
    题目中文计算字符串的长度,类似于String#length。EnglishComputethelengthofastringliteral,whichbehaveslikeString#length.答案解法1typeStringToArr......
  • [Typescript Challenges] 15. Medium - Omit
    Implementthebuilt-in Omit<T,K> genericwithoutusingit.Constructsatypebypickingallpropertiesfrom T andthenremoving KForexampleinterfaceT......