これはここに投稿する必要がありますが、これは私が着陸した場所です...これはバグですか?または機能ですか?わからない
コメントのエラーメッセージは次のように表示されます。なぜこれらのエラーが表示されますか?特別にエラー#1、それは安全でキャストする必要がありますので、TがたITestを拡張するために...
interface ITest {
answer?: number; // error #1 dissapears if '?' is removed
}
class Base<T> {
obj: T;
}
class Derived<T extends ITest> extends Base<T> {
constructor() {
super();
var obj = { answer: 42 };
// ok ... no suprise here
var test:ITest = obj as ITest;
// #1 Neither type { answer: number } or type T is assignable to each other.
var t:T = obj as T;
// this works ... (why? - specially when 1. dont...)
var t2:T = obj as ITest as T;
// #2 Type { answer: number } is not assignable to type 'T'.
// #3 Cannot convert type { answer: number } to type T. Type parameter T is incompatible with { answer: number }, with is not type parameter.
this.obj = obj;
// #4 Type ITest is not assignable to type 'T'.
// #5 Cannot convert type ITest to type T. Type parameter T is incompatible with ITest, with is not type parameter.
this.obj = test;
// this works
this.obj = obj as any;
// in this case error is not shown even if 'answer' is required in ITest ...
// (expected: 'Type { totally: string } is not assignable to type 'T'.)
this.obj = { totally: 'different' } as any;
}
}
歓声