Edu*_*cko 4 getter types typescript typescript2.0
类中的getters是只读属性,因此从下面的代码中抛出类型错误是有意义的.
class Car {
engine: number;
get hp() {
return this.engine / 2;
}
get kw() {
return this.engine * 2;
}
}
function applySnapshot(
car: Car,
snapshoot: Partial<Car> // <-- how to exclude readonly properties?
) {
for (const key in snapshoot) {
if (!snapshoot.hasOwnProperty(key)) continue;
car[key as keyof Car] = snapshoot[key as keyof Car];
// Cannot assign to 'hp' because it is a constant or a read-only property.
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法如何投射可写的属性来键入和排除所有的getter?
Mat*_*hen 19
虽然readonly不直接影响类型是否可分配,但它确实会影响它们是否相同.为了测试两种类型是否相同,我们可以滥用(1)条件类型的可分配性规则,这要求后面的类型extends相同,或者(2)交集类型的推理过程,它从两侧抛出相同的类型.然后我们只使用映射类型,如Titian Cernicova-Dragomir的答案Car,依次查看每个属性,看它是否与自身的可变版本相同.
// https://github.com/Microsoft/TypeScript/issues/27024#issuecomment-421529650
type IfEquals<X, Y, A, B> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? A : B;
// Alternatively:
/*
type IfEquals<X, Y, A, B> =
[2] & [0, 1, X] extends [2] & [0, 1, Y] & [0, infer W, unknown]
? W extends 1 ? B : A
: B;
*/
type WritableKeysOf<T> = {
[P in keyof T]: IfEquals<{ [Q in P]: T[P] }, { -readonly [Q in P]: T[P] }, P, never>
}[keyof T];
type WritablePart<T> = Pick<T, WritableKeysOf<T>>;
class Car {
engine: number;
get hp() {
return this.engine / 2;
}
get kw() {
return this.engine * 2;
}
}
function applySnapshot(
car: Car,
snapshoot: Partial<WritablePart<Car>>
) {
let key: keyof typeof snapshoot;
for (key in snapshoot) {
if (!snapshoot.hasOwnProperty(key)) continue;
car[key] = snapshoot[key];
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 请参阅 @matt-mccutchen 以了解此问题的有趣解决方法。
原答案
readonly是一个相当弱的修饰符,因为它不会影响可分配性。因此,例如,您可以将具有readonly属性的对象分配给具有相同可变属性的对象,并且编译器不会抱怨:
let roCar: Partial<Car> = { hp: 10 } // we can assign a mutable object to a referecne with a readonly property
roCar.hp = 10; // error hp is readonly
//But we can also assign an object with a readonly property to a fully mutable version of it
let allMutableCar: { -readonly [P in keyof Car]: Car[P] } = new Car();
allMutableCar.hp = 10; // No compile time error
Run Code Online (Sandbox Code Playgroud)
这是一个已知问题,记录在此处。
由于此可分配性规则,无法在条件类型中区分只读字段和可变字段之间的区别。
一种解决方法是向只读字段类型添加一些额外的内容。这不会影响您如何使用该字段,但它会给我们一个钩子来移除密钥。
type readonly = { readonly?: undefined };
class Car {
engine!: number;
get hp() : number & readonly {
return this.engine / 2;
}
get kw() : number & readonly {
return this.engine * 2;
}
}
type NoReadonlyKeys<T> = { [P in keyof T]: 'readonly' extends keyof T[P] ? never : P }[keyof T]
type PartialNoReadonly<T> = Partial<Pick<T, NoReadonlyKeys<T>>>
type Mutable<T> = { -readonly [P in keyof T]: T[P] }
function applySnapshot(
car: Car,
snapshoot: PartialNoReadonly<Car>
) {
const mutableCar: Mutable<Car> = car; // erase readonly so we can mutate
for (const key in snapshoot) {
let typedKey = key as keyof typeof snapshoot
if (!snapshoot.hasOwnProperty(key)) continue;
mutableCar[typedKey] = snapshoot[typedKey] as any;
}
}
applySnapshot(new Car(), {
engine: 0
})
applySnapshot(new Car(), {
hp: 0 /// error
})
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1643 次 |
| 最近记录: |