The Wayback Machine - https://web.archive.org/web/20221217212807/https://github.com/microsoft/TypeScript/issues/39681
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Class Property Inference from Constructors fails for a simple object declared in place #39681

Open
simeyla opened this issue Jul 21, 2020 · 2 comments
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Milestone

Comments

@simeyla
Copy link

simeyla commented Jul 21, 2020

TypeScript Version: 4.00-beta and Nightly

Search Terms:
"Class property inference"
"model' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer."

Code

Below are two classes that demonstrate the new Class Property Inference from Constructors in Typescript 4.

The first class calls an instance method to initialize model and the type of model is correctly inferred. LOVE IT!
The first class also demonstrates that a primitive type can be assigned from the constructor (as explained in the announcement).

The second class attempts to do the same, but without a method call. It fails with an error about it being self referencing which it isn't.

class AnimalViewModel_PASS {

    constructor()
    {
        // call a function to get a type - PASS
        this.model = this.createModel();

        // assign simple type directly in constructor - PASS
        this.qty = 10;
    }

    createModel()
    {
        return {
            animal: 'dog',
            sound: 'bark'
        };
    }

    model;  // PASS - type infered as { animal: 'dog', sound: 'bark' }
    qty;    // PASS - type infered as number
}


class AnimalViewModel_FAILS {

    constructor()
    {
        this.model = {
            animal: 'dog',
            sound: 'bark'
        };
    }

    // FAILS
    // 'model' implicitly has type 'any' because it does not have a type 
    // annotation and is referenced directly or indirectly in its own initializer.(7022)
    model;  
}

Expected behavior:
The two classes should compile and type of model should be fully inferred wherever the type is ultimately declared.

Actual behavior:
The second class AnimalViewModel_FAILS shows the following error for model property:

'model' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.(7022)

Playground Link:
Sorry I tried, but there is no Share option on typescript playground. I looked everywhere and tried multiple versions.

Related Issues:
No related issues found

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label Jul 21, 2020
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Jul 21, 2020
@typescript-bot typescript-bot added the Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros label Jul 21, 2020
@ShuiRuTian
Copy link
Contributor

ShuiRuTian commented Jul 24, 2020

OK, I think I got the reason:
Let us call this { animal: 'dog', sound: 'bark' }; as object, and call model as declaration.

The declaration wants to get its type through constructor, so need the type of object.
In the process of getting the type of object(checkExpression in getTypeOfExpression), the code ,not simply return its type, wants to check the type of object, which need the type of declaration.
So, the loop begins......

And thanks to clever TS developers, the loop would not cause ts stops working, but an error.

I would glad to have a try if no others are interested in this.

@typescript-bot
Copy link
Collaborator

typescript-bot commented Apr 13, 2022

👋 Hi, I'm the Repro bot. I can help narrow down and track compiler bugs across releases! This comment reflects the current state of the repro in the issue body running against the nightly TypeScript.


Issue body code block by @simeyla

👍 Compiled

Historical Information
Version Reproduction Outputs
4.4.2, 4.5.2, 4.6.2

👍 Compiled

4.2.2, 4.3.2

Failed: -

  • 'model' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug A bug in TypeScript Has Repro This issue has compiler-backed repros: https://aka.ms/ts-repros
Projects
None yet
Development

No branches or pull requests

4 participants