错误的功能分配

我在非严格模式下遇到问题,这是我的代码,我可以将类型错误的函数分配给类型化变量。

我做错了吗?

谢谢。

interface A {
    f1( ) : void;
}

interface B extends A {
    f2( ) : void;
}

interface C {
    f3( ) : void;
}

type expectA = ( a: A ) => void;
type expectB = ( b: B ) => void;


function testA( a: A ) {
    console.log( "a" );
}

function testB( b: B ) {
    console.log( "b" );
}

function testC( c: C ) {
    console.log( "c" );
}


let v1: expectA = testA;    // ok: assign type A to a type A
let v2: expectA = testB;    // ok: assign type B (extending A) to a type A
let v3: expectA = testC;    // fail -> normal: error TS2322: Type '(c: C) => void' is not assignable to type 'expectA'.
let v4: expectB = testA;    // ok -> **abnormal**: there is no error in !strict mode

以上是错误的功能分配的全部内容。
THE END
分享
二维码
< <上一篇
下一篇>>