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."
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.
classAnimalViewModel_PASS{constructor(){// call a function to get a type - PASSthis.model=this.createModel();// assign simple type directly in constructor - PASSthis.qty=10;}createModel(){return{animal: 'dog',sound: 'bark'};}model;// PASS - type infered as { animal: 'dog', sound: 'bark' }qty;// PASS - type infered as number}classAnimalViewModel_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
The text was updated successfully, but these errors were encountered:
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.
👋 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.
simeyla commentedJul 21, 2020
•
edited by orta
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 ofmodel
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.
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 formodel
property: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
The text was updated successfully, but these errors were encountered: