English 中文(简体)
如何将工会类型改为教师类型
原标题:How to transform union type to tuple type

For example, I have a type:

type abc =  a  |  b  |  c ;

如何在汇编时间时形成包含工会所有要素的图形?

type t = [ a , b ,  c ];
最佳回答

DISCLAIMER: DON T DO THIS!! If someone tells you to use the code they found in this answer to do anything except demonstrate why this is a bad idea, RUN AWAY!!


很容易从图形型转换为工会型;例如,见。 反之,从工会转变为教导,就是其中之一。 Truly Bad Visions, 你应努力这样做。 (见microsoft/Type#13298供讨论和坦率回答) 让我们首先去做,然后::

// oh boy don t do this
type UnionToIntersection<U> =
  (U extends any ? (k: U) => void : never) extends ((k: infer I) => void) ? I : never
type LastOf<T> =
  UnionToIntersection<T extends any ? () => T : never> extends () => (infer R) ? R : never

// TS4.0+
type Push<T extends any[], V> = [...T, V];

// TS4.1+
type TuplifyUnion<T, L = LastOf<T>, N = [T] extends [never] ? true : false> =
  true extends N ? [] : Push<TuplifyUnion<Exclude<T, L>>, L>

type abc =  a  |  b  |  c ;
type t = TuplifyUnion<abc>; // ["a", "b", "c"] 

马达加斯加 游乐场联系

这种工作,但我确实建议不将其用于任何官方目的或任何生产法。 因此:

因此,你走了。 你可以做些什么,但却没有这样做。 (如果你do? ......,如果出现某种探索,则不责怪我?)

问题回答

我有时会遇到这样一种情况,即我想从A类中得出B类,但发现,要么服贸总协定不支持,要么在守则中进行变革,但难以遵循。 有时,从A中选择B是任意的,我也可以从另一个方向获得。 在这里,如果你能够从你的指导开始,你可以很容易地找到一种涵盖图人作为要素接受的所有价值观的类型:

type X = ["a", "b", "c"];

type AnyElementOf<T extends any[]> = T[number];

type AnyElementOfX = AnyElementOf<X>;

如果你检查<代码>AnyElementOfX/code>的扩大情况,请查阅a">> >a" >> > > >> > >。

这种办法需要额外工作,因为从实际教学中得出理想的教学类型。

Um>,, 您正在思考,?

我同意。 然而,它对相关使用案例仍然有用:Ensure that a tuple includes all of the elements of a Union打字 (例如用于单位检测)。 在这种情形下,你需要宣布阵列anyway,因为打字不能产生从类型上得出的时间值。 因此,“外派”工作变得毫无意义。

注:作为先决条件,Im取决于以下网址:,此处讨论。 就完整性而言,此处是使用Im的版本,但还有其他选择:

type FunctionComparisonEqualsWrapped<T> =
  T extends (T extends {} ? infer R & {} : infer R)
  ? { [P in keyof R]: R[P] }
  : never;

type FunctionComparisonEquals<A, B> =
  (<T>() => T extends FunctionComparisonEqualsWrapped<A> ? 1 : 2) extends
   <T>() => T extends FunctionComparisonEqualsWrapped<B> ? 1 : 2
  ? true
  : false;

type IsAny<T> = FunctionComparisonEquals<T, any>;

type InvariantComparisonEqualsWrapped<T> =
  { value: T; setValue: (value: T) => never };

type InvariantComparisonEquals<Expected, Actual> =
  InvariantComparisonEqualsWrapped<Expected> extends
  InvariantComparisonEqualsWrapped<Actual>
  ? IsAny<Expected | Actual> extends true
    ? IsAny<Expected> | IsAny<Actual> extends true
          ? true
          : false
      : true
  : false;

export type TypesEqual<Expected, Actual> =
  InvariantComparisonEquals<Expected, Actual> extends true
  ? FunctionComparisonEquals<Expected, Actual>
  : false;

export type TypesNotEqual<Expected, Actual> =
  TypesEqual<Expected, Actual> extends true ? false : true;

这里是实际解决办法,例如使用:

export function rangeOf<U>() {
  return function <T extends U[]>(...values: T) {
    type Result = true extends TypesEqual<U, typeof values[number]> ? T : never;
    return values as Result;
  };
}

type Letters =  a  |  b  |  c ;
const letters = rangeOf<Letters>()([ a ,  b ,  c ]);
type LettersTuple = typeof letters;

Some caveats: rangeOf does not care about ordering of the tuple, and it allows duplicate entries (e.g. [ b , a , c , a ] will satisfy rangeOf<Letters>()). For unit tests, these issues are likely not worth caring about. However, if you do care, you can sort and de-duplicate values before returning it. This trades a small amount of runtime performance during initialization for a "cleaner" representation.

这可能是可能的,但你必须首先按 as或 des令分类。 你们可以根据自己的需要改变特征的次序,或增加这一榜样的特质。

<斯特隆>Warning:这需要大工会的加权。

/**
 * An array representing alphanumeric characters in ascending order.
 * It includes uppercase letters, lowercase letters, and digits.
 */
export type AlphaNumericAscendingOrder = [
     A ,  B ,  C ,  D ,  E ,  F ,  G ,  H ,  I ,  J ,
     K ,  L ,  M ,  N ,  O ,  P ,  Q ,  R ,  S ,  T ,
     U ,  V ,  W ,  X ,  Y ,  Z ,  a ,  b ,  c ,  d ,
     e ,  f ,  g ,  h ,  i ,  j ,  k ,  l ,  m ,  n ,
     o ,  p ,  q ,  r ,  s ,  t ,  u ,  v ,  w ,  x ,
     y ,  z ,  0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,
     8 ,  9 
];

/**
 * A type that finds the first matching substring in a given union type and returns it.
 * @template Union - The union type to search within.
 * @template Cache - The current cache of matching characters.
 * @template Letters - The array of alphanumeric characters.
 * @returns {string} The first matching substring found in the union type.
 */
export type FindMatch<Union extends string, Cache extends string = "", Letters extends string[] = AlphaNumericAscendingOrder> =
    Cache extends Union
        ? Cache
        : Letters extends [infer Letter, ...infer RemainingLetters]
            ? Letter extends string
                ? Extract<Union, `${Cache}${Letter}${string}`> extends never
                    ? RemainingLetters extends []
                        ? never
                        : FindMatch<Exclude<Union, `${Cache}${Letter}${string}`>, Cache, RemainingLetters extends string[] ? RemainingLetters : never>
                    : FindMatch<Extract<Union, `${Cache}${Letter}${string}`>, `${Cache}${Letter}`>
                : never
            : never

/**
 * A type that converts a union type into a tuple by recursively finding and extracting matching substrings.
 * @template Union - The union type to convert to a tuple.
 * @template Tuple - The resulting tuple.
 * @returns {string[]} A tuple containing all non-overlapping matching substrings from the union type.
 */
export type UnionToTuple<Union extends string, Tuple extends string[] = []> = Exclude<Union, FindMatch<Union>> extends never
    ? [...Tuple, Union]
    : [...Tuple, FindMatch<Union>, ...UnionToTuple<Exclude<Union, FindMatch<Union>>>]

只是一种解决办法。 这很简单,但需要人工书写碎块代码。

至少,你可以肯定,贵方的物品总是为工会的每个成员拥有财产。

export type ThemeName = "default_bootstrap" | "cerulean" | "cosmo" | "cyborg"

type LazyStyleLoader = {
    [key in ThemeName]: () => Promise<typeof import("*?raw")>
}

export const LazyThemeLoader: LazyStyleLoader = {
    default_bootstrap: () => import("bootstrap/dist/css/bootstrap.min.css?raw"),
    cerulean: () => import("bootswatch/dist/cerulean/bootstrap.min.css?raw"),
    cosmo: () => import("bootswatch/dist/cosmo/bootstrap.min.css?raw"),
    cyborg: () => import("bootswatch/dist/cyborg/bootstrap.min.css?raw"),
};

然后,可以使用<条码>目标.keys():

const tuple = Object.keys(LazyThemeLoader);
// ["default_bootstrap", "cerulean", "cosmo", "cyborg"]




相关问题
Converting a Tuples List into a nested List using Python

I want to convert a tuples list into a nested list using Python. How do I do that? I have a sorted list of tuples (sorted by the second value): [(1, 5), (5, 4), (13, 3), (4, 3), (3, 2), (14, 1), (...

Translating python dictionary to C++

I have python code that contains the following code. d = {} d[(0,0)] = 0 d[(1,2)] = 1 d[(2,1)] = 2 d[(2,3)] = 3 d[(3,2)] = 4 for (i,j) in d: print d[(i,j)], d[(j,i)] Unfortunately looping ...

Is there a tuple data structure in Python

I want to have an 3 item combination like tag, name, and list of values (array) what is the best possible data structure to store such things. Current I am using dictionary, but it only allows 2 ...

C# 3.0 Tuple Equivalents (for poor men)

I find myself occasionally in C# 3.0 looking for ways to simulate the notion of a tuple. Over time I ve had various "poor man s" implementations, here are a few of them: Basic Object Array: object[] ...

F# match active pattern as expanded tuple

I get the following error in diff with a red squiggle under Subset. Type mismatch. Expecting a Range -> Choice but given a Range * Range -> Choice Is there some sort of type annotation I can ...

Why can t I sort this list?

statlist = [( abc ,5,1), ( bzs ,66,1), ... ] sorted(statlist, key=lambda x: int(x[1])) I want to sort it by the integer largest to smallest. In this case, 5 and 66. But it doesn t seem to be working.

Why can t I join this tuple in Python?

e = ( ham , 5, 1, bird ) logfile.write( , .join(e)) I have to join it so that I can write it into a text file.