简体   繁体   English

打字稿中的动态键和值类型检查

[英]Dynamic Keys and value type checking in typescript

I have started learning typescript.我已经开始学习打字稿。 I am sure there might be a similar ticket as this but wanted a quick view into this.我确信可能有与此类似的票证,但想快速了解一下。

i have keys
type keys = 'a' | 'e' | 'i' | 'o' | 'u';
I want these to restrict the possible a keys in an object
{
numbers : number;
symbols : string;
[key : keys] : string | number | boolean;
}

However i get this error An index signature parameter type cannot be a union type. Consider using a mapped object type instead.但是我收到这个错误An index signature parameter type cannot be a union type. Consider using a mapped object type instead. An index signature parameter type cannot be a union type. Consider using a mapped object type instead. so i tried所以我试过了

{
numbers : number;
symbols : string;
[key in keys] : string | number | boolean;
}

but I got another error from TSLint.但是我从 TSLint 那里得到了another error I hope if somebody can help me out with these with examples why and how do they vary?我希望如果有人可以通过示例帮助我解决这些问题,它们为什么以及如何变化? and solution Please.和解决方案请。

The result I want is我想要的结果是

key with only values of a, e, i, o, u and these Keys can have any of the string, number, boolean types of values.

Mapped types are similar to index signatures , but they are not the same. 映射类型类似于索引签名,但它们并不相同。 As you discovered, the keys of index signatures can only be exactly string or number .正如您所发现的,索引签名的键只能是stringnumber Mapped types allow you to specify a narrower set of keys, but you can't add other properties to a mapped type.映射类型允许您指定一组更窄的键,但您不能向映射类型添加其他属性。

What you can do is use an intersection :可以做的是使用一个交集

type X = {numbers: number; symbols: string} & {[K in keys]: string | number | boolean}

// type X = {
//  numbers: number;
//  symbols: string;
// } & {
//  a: string | number | boolean;
//  e: string | number | boolean;
//  i: string | number | boolean;
//  o: string | number | boolean;
//  u: string | number | boolean;
// }

This is essentially the same as what you want, since the intersection A & B means that you must conform to both A and B .这与您想要的基本相同,因为A & B的交集意味着您必须同时符合AB

Another way to represent this type is to use a mapped conditional type for the whole thing instead of an intersection.表示这种类型的另一种方法是对整个事物使用映射条件类型而不是交集。 This will be more complicated-looking for you, but evaluates to something nicer for people to use:这对您来说看起来会更复杂,但评估为更适合人们使用的东西:

type Y = { [K in "numbers" | "symbols" | keys]:
  K extends "numbers" ? number :
  K extends "symbols" ? string :
  string | number | boolean };

// type Y = {
// a: string | number | boolean;
// e: string | number | boolean;
// i: string | number | boolean;
// o: string | number | boolean;
// u: string | number | boolean;
// numbers: number;
// symbols: string;

Either way should work.无论哪种方式都应该有效。 Hope that helps;希望有所帮助; good luck!祝你好运!


    type keys =  'a' | 'e' | 'i' | 'o' | 'u'

    type Dynamic = {
      [index in  keys]: string | number | boolean
    }&{
      numbers : number;
      symbols : string;
    }

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

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