microsoft / microsoft/TypeScript

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

Open
#8,474 17 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Needs Proposal Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by compiling and running the TypeScript example in the issue, then read the discussion about the intended inheritance behavior. A viable contribution would need an agreed language design, an implementation plan, and regression coverage showing the resulting behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
20/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.