Type Assertion(型アサーション)
var foo = {};
foo.bar = 123; // Error: property 'bar' does not exist on `{}`
foo.bas = 'hello'; // Error: property 'bas' does not exist on `{}`interface Foo {
bar: number;
bas: string;
}
var foo = {} as Foo;
foo.bar = 123;
foo.bas = 'hello';as fooと<foo>の違い
as fooと<foo>の違いvar foo: any;
var bar = <string> foo; // bar is now of type "string"型アサーションとキャスト
アサーションは害
ダブルアサーション
TypeScriptが単一のアサーションが不十分と判断する方法
最終更新