简体   繁体   English

打字稿减去文字数字类型

[英]typescript substract literal number types

TLDR:域名注册地址:

something like:就像是:

type type4= 5-1 //type 4
// also ok
type type4= '5'-'1' // type '4'

how can I achieve something similar?我怎样才能实现类似的目标?

details细节

I want to build a generic function type that receives number literal (or string literal) and returns unions of numbers until '0' .我想构建一个通用函数类型,它接收数字文字(或字符串文字)并返回数字的联合,直到'0' for example if '3' was passed then the return would be '0'|'1'|'2' .例如,如果'3'被传递,那么返回将是'0'|'1'|'2' lets call this generic RangeUnion .让我们称之为通用RangeUnion

type Union01 = RangeUnion<'1'> // should be '0'|'1'  - RangeUnion should be implemented

Why?为什么?

this is only for reference and not necessary to understand in order to answer my specific question on top.这仅供参考,不需要理解以回答我的具体问题。 if you don't have an answer to the question above, maybe you can help with some idea for different implementation.如果您对上述问题没有答案,也许您可​​以为不同的实现提供一些想法。

I'm building developers API for react component and I want to create sophisticated type inferring using typescript.我正在为 React 组件构建开发人员 API,我想使用打字稿创建复杂的类型推断。 without going to much into details: I have array of types when each item in the array depends on prior items in the array.无需过多赘述:当数组中的每个项目都依赖于数组中的先前项目时,我有类型数组。 i want to create intersection of the types until the given type in the array, for example:我想创建类型的交集,直到数组中的给定类型,例如:

// utilities
type GetIndex<A extends any[], T> = {
  [K in keyof A]: A[K] extends T ? K : never;
}[number];
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;

// actual
type Arr = [{ type0: any }, { type1: any }, { type2: any },]
type Type1 = GetIndex<Arr, { type1: any }> // type is '1' - good
type Union01 = RangeUnion<Type1> // should be 0|1 - RangeUnion should be implemented
type DesiredType = UnionToIntersection<Arr[Union01]> //type is { type0: any } & { type1: any }

Playground 操场

thank you very much @robert and @captain-yossarian your answers help wrote the complete solution I was needed, I did slight modifications:非常感谢@robert 和@captain-yossarian,您的回答帮助我编写了我需要的完整解决方案,我做了一些修改:

// utils
type GetIndex<Arr extends Array<any>, T> = Arr extends [...infer Tail, infer Last]
  ? Last extends T
    ? Tail["length"]
    : GetIndex<Tail, T>
  : never;
type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
type BuildTuple<L extends number, T extends any[] = []> = T extends { length: L } ? T : BuildTuple<L, [...T, any]>;
type RangeUnion<N extends number, Result extends Array<unknown> = []> = Result["length"] extends N
  ? Result[number]
  : RangeUnion<N, [...Result, Result["length"]]>;

// actual
type Arr = [{ type0: any }, { type1: any }, { type2: any }];
type Type2 = GetIndex<Arr, { type2: any }>; 
type Union01 = RangeUnion<Type2>; 
type DesiredType = UnionToIntersection<Arr[Union01]>; //type is { type0: any } & { type1: any }
// works!

anyone needing explicitly use addition or subtraction of literal number types in typescript can see it in the great article @Roberto Zvjerković commented任何需要在打字稿中明确使用文字数字类型的加法或减法的人都可以在@Roberto Zvjerković 评论的伟大文章中看到它

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM