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

Typescript类型体操 - Chainable Options

时间:2022-09-04 21:44:58浏览次数:89  
标签:Typescript option get Chainable value key type Options

题目

中文

在 JavaScript 中我们经常会使用可串联(Chainable/Pipeline)的函数构造一个对象,但在 TypeScript 中,你能合理的给它赋上类型吗?

在这个挑战中,你可以使用任意你喜欢的方式实现这个类型 - Interface, Type 或 Class 都行。你需要提供两个函数 option(key, value)get()。在 option 中你需要使用提供的 key 和 value 扩展当前的对象类型,通过 get 获取最终结果。

例如

declare const config: Chainable

const result = config
  .option('foo', 123)
  .option('name', 'type-challenges')
  .option('bar', { value: 'Hello World' })
  .get()

// 期望 result 的类型是:
interface Result {
  foo: number
  name: string
  bar: {
    value: string
  }
}

你只需要在类型层面实现这个功能 - 不需要实现任何 TS/JS 的实际逻辑。

你可以假设 key 只接受字符串而 value 接受任何类型,你只需要暴露它传递的类型而不需要进行任何处理。同样的 key 只会被使用一次。

English

Chainable options are commonly used in Javascript. But when we switch to TypeScript, can you properly type it?

In this challenge, you need to type an object or a class - whatever you like - to provide two function option(key, value) and get(). In option, you can extend the current config type by the given key and value. We should about to access the final result via get.

For example

declare const config: Chainable

const result = config
  .option('foo', 123)
  .option('name', 'type-challenges')
  .option('bar', { value: 'Hello World' })
  .get()

// expect the type of result to be:
interface Result {
  foo: number
  name: string
  bar: {
    value: string
  }
}

You don't need to write any js/ts logic to handle the problem - just in type level.

You can assume that key only accepts string and the value can be anything - just leave it as-is. Same key won't be passed twice.

答案

type Chainable<TChain = {}> = {
  option<TKey extends string, T>(
    key: TKey extends keyof TChain ? (T extends TChain[TKey] ? never : TKey) : TKey,
    value: T): Chainable<{ [K in (keyof TChain extends TKey ? never : keyof TChain)]: TChain[K] } & { [K in TKey]: T }>;
  get(): TChain;
}

在线演示

标签:Typescript,option,get,Chainable,value,key,type,Options
From: https://www.cnblogs.com/laggage/p/type-challenge-chainable-options.html

相关文章

  • Typescript类型体操 - Tuple To Union
    题目中文实现泛型TupleToUnion<T>,它返回元组所有值的合集。例如typeArr=['1','2','3']typeTest=TupleToUnion<Arr>//expectedtobe'1'|'2'|'3'Eng......
  • [Typescript] 14. Easy - Parameters
    Implementthebuilt-inParametersgenericwithoutusingit.Forexample:constfoo=(arg1:string,arg2:number):void=>{}typeFunctionParamsType=MyPara......
  • [Typescript] 11. Medium - Equal
    ImplementtheEqual<T,U>Forexample:typeisEqual=Equal<1,1>//trueIdea: Parametertype: <P>(x:P)=>anyCheckPextendsT?1:2ThencheckPexte......
  • [Typescript] 12. Easy - Push
     Implementthegenericversionof Array.pushForexample:typeResult=Push<[1,2],'3'>//[1,2,'3'] /*_____________YourCodeHere_____________*/......
  • [Typescript] 13. Easy - Unshift
    Implementthetypeversionof Array.unshiftForexample:typeResult=Unshift<[1,2],0>//[0,1,2,] /*_____________YourCodeHere_____________*/t......
  • Typescript类型体操 - Deep Readonly
    题目中文实现一个通用的DeepReadonly<T>,它将对象的每个参数及其子对象递归地设为只读。您可以假设在此挑战中我们仅处理对象。数组,函数,类等都无需考虑。但是,您仍然可以......
  • Typescript类型体操 - First of Array
    题目中文实现一个通用First<T>,它接受一个数组T并返回它的第一个元素的类型。例如:typearr1=['a','b','c']typearr2=[3,2,1]typehead1=First<arr1>//e......
  • Typescript类型体操 - Length of Tuple
    题目中文创建一个通用的Length,接受一个readonly的数组,返回这个数组的长度。例如:typetesla=['tesla','model3','modelX','modelY']typespaceX=['FALCON9'......
  • Typescript类型体操 - Tuple To Object
    题目中文传入一个元组类型,将这个元组类型转换为对象类型,这个对象类型的键/值都是从元组中遍历出来。例如:consttuple=['tesla','model3','modelX','modelY']a......
  • [Typescript Challenges] 10. Medium - Include
    ImplementtheJavaScript Array.includes functioninthetypesystem.Atypetakesthetwoarguments.Theoutputshouldbeaboolean true or false.Forexa......