Repository navigation
3.3.1 regression in type inference #29743
Copy link
Copy link
Open
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolution
Milestone
Description
TypeScript Version: 3.3.1 & 3.4.0-dev.20190202
Search Terms:
Code
class Foo {
foo: number = 0;
}
class FooBar extends Foo {
bar: number = 10;
}
async function foo(f: Foo | number) {
if (f instanceof FooBar) {
try {
f = await (async (): Promise<Foo | number> => {
throw new Error();
})();
}
catch (ex) {
console.log(f.bar); // 3.3.1 has an error on this line -- "Property 'bar' does not exist on type 'number | Foo'. Property 'bar' does not exist on type 'number'"
}
}
}
foo(new FooBar());Expected behavior:
No error as was in TypeScript 3.2.4
Actual behavior:
Error Property 'bar' does not exist on type 'number | Foo'. Property 'bar' does not exist on type 'number'
Related Issues:
Reacted by Alex Sharp and Spencer Tuft
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptDomain: check: Type InferenceRelated to type inference performed during signature resolution or `infer` type resolutionRelated to type inference performed during signature resolution or `infer` type resolution
Activity
eamodio commentedon Feb 5, 2019
Here is another case
Code
Expected behavior:
No error as was in TypeScript 3.2.4
Actual behavior:
Error
Object is possibly 'undefined'Playground Link:
http://www.typescriptlang.org/play/index.html#src=function%20foo(o%3A%20%7B%20item%3F%3A%20%7B%20message%3F%3A%20string%20%7D%20%7D)%20%7B%0D%0A%20%20%20%20if%20(o.item%20%3D%3D%3D%20undefined%20%7C%7C%20o.item.message%20%3D%3D%3D%20undefined)%20%7B%0D%0A%20%20%20%20%20%20%20%20o.item%20%3D%20(()%20%3D%3E%20(%7B%20message%3A%20'hello'%20%7D))()%3B%0D%0A%20%20%20%20%7D%0D%0A%0D%0A%20%20%20%20console.log(o.item.message.length)%3B%0D%0A%7D
RyanCavanaugh commentedon Feb 5, 2019
See #29513 for
instanceofissueseamodio commentedon Feb 5, 2019
Ryan Cavanaugh (@RyanCavanaugh) that issue says it affects 3.2+, but I only see the issue with 3.3+, prior to 3.3 those cases worked fine for me.
ahejlsberg commentedon Feb 5, 2019
This is not related to #29513. I believe it is caused by #29466.
ajsharp commentedon Mar 15, 2019
Any movement on this? I came across this today and it bit me in production. Had tests around the affected code but test suite was running via ts-node, so it didn't catch this.
ajsharp commentedon Mar 15, 2019
To add some detail on my particular issue (happy to open a separate issue if unrelated), this issue seems related to importing certain kinds of interfaces, possibly related to #13965. At least in my case, classes are fine. Specifically, the interface doesn't get brought into the imported javascript, but the class does.
I put together a little repo that illustrates the bug: https://github.com/ajsharp/typescript-interface-bug.
Below is the relevant part of the code.
Documentis an exported interface from mongoose, andQueryis an exported class. Here's the type definition forDocument.https://github.com/DefinitelyTyped/DefinitelyTyped/blob/e51e5044b8cf1bb317f5ca052cba4531d051a234/types/mongoose/index.d.ts#L3301
Which generates the following javascript, which does not properly import the
Documentinterface.ajsharp commentedon Mar 19, 2019
ping Ryan Cavanaugh (@RyanCavanaugh) Anders Hejlsberg (@ahejlsberg)
ajsharp commentedon Mar 20, 2019
Following up here after some conversation in the typescript gitter. I was reminded that interfaces do not show up in compiled code. However, the compiler should be raising a
'Document' only refers to a type, but is being used as a value hereerror due to how I'm trying to use it, but it is not.This behavior seems limited to imported modules, not local code. If I define an interface in my project and try to use it on the right side of an
instanceofstatement, the typescript compiler raises the proper error. But when importing an interface from an external module, it does not.