简体   繁体   English

TypeScript:`'keyof R'` 不可分配给类型'string',因为它可以是'string | 编号 | 象征'

[英]TypeScript: `'keyof R'` is not assignable to type 'string' because it can be either 'string | number | symbol'

I have a function that takes a key of an object, is there a way to narrow the type of the key to only string我有一个 function,它采用 object 的键,有没有办法将键的类型缩小为仅string

function foo<R, K extends keyof R>(obj: R, key: K, array: string[]) {
  array.push(key); // Error🚨: Type 'string | number | symbol' is not assignable to type 'string'.
    
  // omit implementation details...
  return obj[key];
}

So TS compiler says that 'keyof R' is not assignable to type 'string' because it can be either 'string |所以 TS 编译器说'keyof R'不能分配给类型 'string' 因为它可以是 'string | number |编号 | symbol'.象征'。 I wonder if there is a way to narrow its type to only string so we can exclude number and symbol from it.我想知道是否有办法将其类型缩小为仅string ,以便我们可以从中排除numbersymbol

I tried this but it seems wrong syntactically我试过了,但在语法上似乎是错误的

function foo<R, K extends Extract(keyof R, string)>(obj: R, key: K, array: string[]) {
  array.push(key);

  // omit implementation details...
  return obj[key];
}

Your generic syntax is wrong, Extract(A, B) needs to be Extract<A, B> , so function foo<R, K extends Extract<keyof R, string>>(obj: R, key: K, array: string[]) .你的通用语法是错误的, Extract(A, B)需要是Extract<A, B> ,所以 function foo<R, K extends Extract<keyof R, string>>(obj: R, key: K, array: string[])

That said there is really no reason to have your function generic on two things when you care about one object:也就是说,当您关心一个object 时,真的没有理由让您的 function 在件事上通用:


function foo<R>(obj: R, key: Extract<keyof R, string>, array: string[]): void {
  array.push(key);
}

Would work just as well and is much simpler.效果一样好,而且简单得多。

You need the second type if you want to return the object's value at the key, and you end up with:如果你想在键处返回对象的值,你需要第二种类型,你最终会得到:

function foo<R, T extends Extract<keyof R, string>>(obj: R, key: T, array: string[]): R[T] {
  array.push(key);
  return obj[key];
}
foo({x: 3, y: 5}, 'x', array);

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

相关问题 Typescript 类型字符串不可分配给类型键“接口” - Typescript type string is not assignable to type keyof "Interface" "打字稿类型字符串不可分配给类型 keyof" - Typescript type string is not assignable to type keyof 为什么`keyof any`具有`string |类型? 编号 打字稿中的符号`? - Why does `keyof any` have type of `string | number | symbol` in typescript? Typescript - 类型“未定义”不可分配给类型“字符串 | 号码 | 象征' - Typescript - Type 'undefined' is not assignable to type 'string | number | symbol' 输入'字符串| 编号 | symbol' 不可分配给类型 'string' - Type 'string | number | symbol' is not assignable to type 'string' Typescript 使用 keyof 时,索引签名参数类型必须是“字符串”或“数字” - Typescript An index signature parameter type must be either 'string' or 'number' when using keyof 类型“接口”不可分配给类型“字符串 | 编号 | 象征' - Type 'Interface' is not assignable to type 'string | number | symbol' 字符串不可分配给keyof只读 <T> 打字稿 - String is not assignable to keyof Readonly<T> typescript 类型 'string' 不可分配给类型 'keyof FitEnum | 不明确的' - Type 'string' is not assignable to type 'keyof FitEnum | undefined' 类型“字符串”不可分配给类型“keyof CustomerInput” - Type 'string' is not assignable to type 'keyof CustomerInput'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM