microsoft / microsoft/TypeScript

Inferred missing properties use resolved generic types rather than staying generic

Open
#29,183 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Domain: LS: Quick Fixes Experience Enhancement Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.3.0-dev.20181212

Search Terms: declare property generic missing member

Code

class TestClass<T> {
    // Inferred:
    // otherValue: number;
    public constructor(
        public readonly value: T,
    ) { }
}


const foo = new TestClass<number>(1);

foo.otherValue = foo.value;

Expected behavior:

Declare property 'otherValue' should create otherValue: T;

Actual behavior:

...but it actually creates otherValue: number. Whatever generic type from the place you request the missing property be added will be the type of the property.

Playground Link: http://www.typescriptlang.org/play/#src=class%20TestClass%3CT%3E%20%7B%0D%0A%20%20%20%20%2F%2F%20Inferred%3A%0D%0A%20%20%20%20%2F%2F%20otherValue%3A%20number%3B%0D%0A%20%20%20%20public%20constructor(%0D%0A%20%20%20%20%20%20%20%20public%20readonly%20value%3A%20T%2C%0D%0A%20%20%20%20)%20%7B%20%7D%0D%0A%7D%0D%0A%0D%0A%0D%0Aconst%20foo%20%3D%20new%20TestClass%3Cnumber%3E(1)%3B%0D%0A%0D%0Afoo.otherValue%20%3D%20foo.value%3B%0D%0A

Related Issues: I found a lot of similar bugs but didn't see any that looked related...

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 with the provided TypeScript playground reproduction using typescript@next and verify whether the behavior still occurs. Trace how the missing property is inferred from the generic class and instantiated value. Done means the declared property preserves the generic type T rather than resolving to number, with a regression test covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.