we know that typescript applies structure typing as below example shows:
interface Vector {
x: number;
y: number;
}
interface NamedVector {
x: number;
y: number;
name: string;
}
function calculateLength(v: Vector) {
return Math.sqrt(v.x * v.x + v.y * v.y);
}
const v: NamedVector = { x: 3, y: 4, name: 'zee' };
calculateLength(v); // compile OK, result is 5
It allowed calculateLength
to be called with a NamedVector
because its structure was compatible with Vector
.
But in term of assignment, it doesn’t use structure typing anymore:
const v: Vector = { x: 3, y: 4, name: 'Zee' }; // compile error, 'name' does not exist in type 'Vector'
according to the definition of structure typing, { x: 3, y: 4, name: 'Zee' }
is also compatible with Vector
, so why structure typing doesn’t work here?
and what is the Utility Types I can use to describe a type that must contains x
and y
fields and also some other fields so I can do:
const v: XXX<Vector> = { x: 3, y: 4, name: 'Zee' };
3
Answers
To answer your second question
TypescriptPlayground
The first question is not specific enough to answer, in my opinion.
I cannot think of a single use-case where this would be make sense but the simplest way to down-cast is to, well, cast
Of course you won’t be able to access
v.name
so again, this seems a pointless exercise.You should not use excess property when use Object literal. So you should use Custom Genernics not to use excess property.