Given the following interface:
interface Request
{
name: string;
email: string;
}
I would have thought that the following lines of code were functionally identical at design time:
var request1: Request = {name: "John"};
var request2 = {name: "John"} as Request;
But they are not. The second line compiles, whereas the first complains that the email
property is missing.
If Typescript believes that all non-optional properties of the type must be specified, why doesn't it complain about type assertion using the as Request
on an object that's missing a property.
And is there a casting syntax that would enforce this stronger typing? (The <Request>(...)
syntax doesn't do so either.)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…