Typescript type guard for requiring all elements

前端 未结 3 1948
旧时难觅i
旧时难觅i 2021-01-21 17:54

Is there a way to require array elements with typescript so that I can do have

type E = keyof T; // Number of properties in T is unknown

let T

3条回答
  •  -上瘾入骨i
    2021-01-21 18:44

    You can create a tuple:

    type E = "el1"|"el2"|"el3";
    
    type ITestElement = {
        arg: T
    };
    
    type ITestElements = [ITestElement<"el1">, ITestElement<"el2">, ITestElement<"el3">];
    
    

提交回复
热议问题