[英]How to force read-only arrays to have a tuple type with string literals in Typescript?
I have a configuration object as follows: 我有一个配置对象,如下所示:
const config = { envs: ['dev', 'test', 'prod'], targets: ['> 2%'] };
Currently, the typescript compiler infers the type of this object to be: 当前,打字稿编译器推断该对象的类型为:
type IConfig = { envs: string[], targets: string[] };
Which makes sense as I might mutate this array at some point after declaration. 这很有意义,因为我可能在声明后的某个时候对这个数组进行了变异。
I'm not going to change it though, so I'd like it to be: 我不会更改它,所以我希望它是:
type IConfig = { envs: ['dev', 'test', 'prod'], targets: ['> 2%'] };
Is there any way to tell the compiler to infer the type of config.envs
as a tuple type with string literals (without typing it out)? 有什么方法可以告诉编译器将config.envs
的类型推断为带有字符串文字的元组类型(无需键入)?
Edit: The best answer is 90% of the way there, but I'm hoping for a method that can be applied to the object as a whole rather than each of the properties. 编辑:最好的答案是方法的90%,但我希望可以将一种方法应用于整个对象,而不是每个属性。 I've added another property to the examples to make this clearer. 我在示例中添加了另一个属性,以使其更加清晰。
Combining the standard trick to infer literal types with a trick I just learned to infer a tuple type rather than an array type : 我刚刚学会了推断元组类型而不是数组类型的方法,将用于推断文字类型的标准技巧与之相结合:
function asTupleOfLiterals<T extends string, U extends [T, ...T[]]>(tuple: U): U {
return tuple;
}
const config = { envs: asTupleOfLiterals(['dev', 'test', 'prod']) };
Miraculously, if we just wrap [T, ...T[]]
in an object with an index signature, the contextual typing seems to work: 奇迹般地,如果我们仅将[T, ...T[]]
包装在具有索引签名的对象中,则上下文类型似乎可以工作:
function asObjectOfTuplesOfLiterals<T extends string,
U extends {[n: string]: [T, ...T[]]}>(obj: U): U { return obj; }
const config = asObjectOfTuplesOfLiterals(
{ envs: ['dev', 'test', 'prod'], targets: ['> 2%'] });
FTR, there's an open suggestion to make it easier to infer literal types and one to make it easier to infer tuple types . FTR,有一个开放的建议,可以使推断文字类型更容易,而又可以使推断元组类型更容易 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.