TypeScript inheritence - 当参数是兄弟子类时,为什么我没有得到类型错误?

时间:2016-10-25 20:31:04

标签: inheritance typescript

鉴于此代码,可在此处尝试:

export abstract class AbstractSanitizedString {    
    constructor(private s: string) {
    }    
}

export class PersonNickname extends AbstractSanitizedString {
    constructor(label: string) {
        super(label + " Smith");
    }    
}

export class AlphaNumericString extends AbstractSanitizedString {
    constructor(s: string) {
        super(s);
    }
}

export class Person {    
    constructor(private name: PersonNickname) {
    }    
}

new Person(new AlphaNumericString("John"));

the code in playground

我预计会出现编译错误,因为我发送了AlphaNumericString,而我定义它只应该接收PersonNickname

这是对的吗?

0 个答案:

没有答案