我需要通过字符串属性名称来更新类的属性的值。我首先通过以下方法确保属性名称有效:
export class ClientDTO {
...
static isValidPropertyName(name: string): name is keyof ClientDTO {
return (name as keyof ClientDTO) !== undefined
}
}
然后在另一堂课中,我正在这样做:
foo(key: string, newValue: string) {
if (!ClientDTO.isValidPropertyName(key)) {
return
}
if (newValue !== this.originalClient[key]) {
// @ts-ignore
this.originalClient[key] = newValue
}
}
The lookup works well now, but to do the update I'm having to put the // @ts-ignore
there and I'd really like to figure out how to do this properly without having to have the ignore there.
我已经进行了严格的检查,所以我得到了错误
TS2322:类型“ any”不能分配给类型“ never”
This doesn't check that
name
is a key of ClientDTO. It asserts that it is, and then checks whether the string is undefined. Try it in the playground.Even if that worked, it would only check that the string is a valid key of ClientDTO, but does not say which one it is. Therefore, Typescript checks that the type you're setting is safely assignable to any key of ClientDTO; since ClientDTO contains "a mix of types" including "String, Boolean, date and number", the only safe value to assign is
never
.For you to safely assign a
newValue: string
, you'll need a function that ensures at runtime that yourkey
is for astring
-typed property, which might involve some duplication.typescript playground
See also: Typescript error:Type 'number' is not assignable to type 'never'