简体   繁体   English

Typescript 使用 keyof 时,索引签名参数类型必须是“字符串”或“数字”

[英]Typescript An index signature parameter type must be either 'string' or 'number' when using keyof

Using Typescript, I am trying to write a function that takes in a generic object and a key value mapping of validation rules to apply on each property.使用 Typescript,我正在尝试编写一个 function,它采用通用 object 和验证规则的键值映射以应用于每个属性。 When using the keyof property to verify that keys passed in the "validators" object are in the TObject generic, I get An index signature parameter type must be either 'string' or 'number' .当使用 keyof 属性验证“验证器”object 中传递的密钥是否在TObject泛型中时,我得到An index signature parameter type must be either 'string' or 'number' Later, when attempting to access a value in validators , I get No index signature with a parameter of type 'string' was found on type '{}' .后来,当尝试访问validators中的值时,我得到No index signature with a parameter of type 'string' was found on type '{}'

function isValid<TObject>(
  o: TObject,
  validators: {
    [key: keyof TObject]: (o: TObject) => boolean;
  }
) {
  for (const validator in validators) {
    if (!validators[validator](o)) {
      return false;
    }
  }

  return true;
}

type Todo = {
  text: string;
  details: string;
  completed: boolean;
};

console.log(
  isValid<Todo>(
    {
      text: "mow yard",
      details: "needs to be completed asap",
      completed: false
    },
    {
      text: (o: Todo) => o.text.length > 0,
      completed: (o: Todo) => !!o.completed
    }
  )
);

You should use the keyword in instead of : , as : is for index type (string, number, symbol)您应该使用关键字in而不是: ,因为:用于索引类型(字符串、数字、符号)

function isValid<TObject>(
  o: TObject,
  validators: {
    [key in keyof TObject]: (o: TObject) => boolean;
  }
) {
  for (const validator in validators) {
    if (!validators[validator](o)) {
      return false;
    }
  }

  return true;
}

if a key is optional, use ?如果键是可选的,请使用? :

  validators: {
    [key in keyof TObject]?: (o: TObject) => boolean;
  }

暂无
暂无

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

相关问题 Typescript 索引签名参数类型必须是“字符串”或“数字” - Typescript An index signature parameter type must be either 'string' or 'number' 索引签名参数类型必须是“字符串”或“数字” - An index signature parameter type must be either 'string' or 'number' TypeScript:尝试使用字符串时,索引签名参数必须是“字符串”或“数字”| 数字 - TypeScript: An index signature parameter must be a 'string' or 'number' when trying to use string | number TypeScript:尝试访问枚举时,没有带有“字符串”类型参数的索引签名 - TypeScript: No index signature with a parameter of type 'string' when try to access the Enum 打字稿 - 没有带有“字符串”类型参数的索引签名 - Typescript - No index signature with a parameter of type 'string' TypeScript:`'keyof R'` 不可分配给类型'string',因为它可以是'string | 编号 | 象征' - TypeScript: `'keyof R'` is not assignable to type 'string' because it can be either 'string | number | symbol' Typescript Generics keyof参数签名 - Typescript Generics keyof parameter signature Typescript: 在类型 '{ "A": string; 上找不到参数类型为 'string' 的索引签名 } - Typescript: No index signature with a parameter of type 'string' was found on type '{ "A": string; } 在“{}”类型上找不到具有“数字”类型参数的索引签名 - Typescript 编译器错误 - No index signature with a parameter of type 'number' was found on type '{}' - Typescript compiler error 打字稿:在类型“{}”上找不到带有“字符串”类型参数的索引签名 - Typescript: No index signature with a parameter of type 'string' was found on type '{}'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM