microsoft / microsoft/TypeScript

Overriden class member annotated with a subtype could be a recipe for disaster (by design.. ;) )

未关闭
#8,474 17 条评论 3 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

Needs Proposal Suggestion
主要语言
Go
星标
111k
派生
14.3k
平均合并
2 天 4 小时
30 天内合并 PR
132

描述

TypeScript Version:
1.9.0-dev.20160505 with both strictNullChecks and noImplicitAny enabled.

Code:

class Animal {
    name: string = "Any animal";
}

class Dog extends Animal {
    name: string = "Dog";

    bark() {
        console.log("bark");
    }
}

class AnimalCage {
    member: Animal = new Animal();
}

class DogCage extends AnimalCage {
    member: Dog;
}

let dogCage = new DogCage();

if (dogCage.member != null) { // Yup, it is even checked for null or undefined, 
                              // but it turned out that wasn't enough..
    dogCage.member.bark(); // Bang!
}

Expected behavior:
Design a language where inheritance actually works.
(Edit: I was felling very 'passionate' when I wrote this.. sorry :) nothing personal)

Actual behavior:
By design:

    dogCage.member.bark();
                   ^

TypeError: dogCage.member.bark is not a function
    at Object.<anonymous> (C:\X\Documents\Drafts\TypeScript\Code\a.js:38:20)
    at Module._compile (module.js:541:32)
    at Object.Module._extensions..js (module.js:550:10)
    at Module.load (module.js:456:32)
    at tryModuleLoad (module.js:415:12)
    at Function.Module._load (module.js:407:3)
    at Function.Module.runMain (module.js:575:10)
    at startup (node.js:159:18)
    at node.js:444:3

Resolution:
tslint volunteers, for the rescue!

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

先编译并运行 issue 中的 TypeScript 示例,然后阅读关于预期继承行为的讨论。一项可行的贡献需要一份达成共识的语言设计、一份实现计划,以及能够展示最终行为的回归覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
typescript
领域
compilers
Issue 类型
功能
难度
5/5
预计耗时
一周以上
活跃度
停滞
描述清晰度
需要澄清
新手友好度
20/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。