-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
3.3.1 regression in type inference #29743
Comments
Here is another case Code function foo(o: { item?: { message?: string } }) {
if (o.item === undefined || o.item.message === undefined) {
o.item = (() => ({ message: 'hello' }))();
}
console.log(o.item.message.length);
} Expected behavior: No error as was in TypeScript 3.2.4 Actual behavior: Error |
See #29513 for |
@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. |
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. |
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. import { model, Document, Model, Schema, Query } from "mongoose";
export interface PersonDocument extends Document {
name: string;
}
export interface PersonModel extends Model<PersonDocument> {}
const personSchema = new Schema({
name: String
})
personSchema.methods.test = function() {
// will throw a 'ReferenceError: Document is not defined' error
if (this instanceof Document) {
return `I'm a document!`;
} else if (this instanceof Query) {
return `I'm a query!`
} else {
return `I'm neither`
}
}
export const Person: PersonModel = model<PersonDocument, PersonModel>('Person', personSchema); Which generates the following javascript, which does not properly import the "use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const mongoose_1 = require("mongoose");
const personSchema = new mongoose_1.Schema({
name: String
});
personSchema.methods.test = function () {
// will cause a Document undefined error
if (this instanceof Document) {
return `I'm a document!`;
}
else if (this instanceof mongoose_1.Query) {
return `I'm a query!`;
}
else {
return `I'm neither`;
}
};
exports.Person = mongoose_1.model('Person', personSchema);
//# sourceMappingURL=Person.js.map |
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 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 |
TypeScript Version: 3.3.1 & 3.4.0-dev.20190202
Search Terms:
Code
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'
Playground Link:
http://www.typescriptlang.org/play/index.html#src=class%20Foo%20%7B%0D%0A%20%20%20%20foo%3A%20number%20%3D%200%3B%0D%0A%7D%0D%0A%0D%0Aclass%20FooBar%20extends%20Foo%20%7B%0D%0A%20%20%20%20bar%3A%20number%20%3D%2010%3B%0D%0A%7D%0D%0A%0D%0Aasync%20function%20foo(f%3A%20Foo%20%7C%20number)%20%7B%0D%0A%20%20%20%20if%20(f%20instanceof%20FooBar)%20%7B%0D%0A%20%20%20%20%20%20%20%20try%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20f%20%3D%20await%20(async%20()%3A%20Promise%3CFoo%20%7C%20number%3E%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20throw%20new%20Error()%3B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D)()%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%20%20%20%20catch%20(ex)%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20console.log(f.bar)%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Afoo(new%20FooBar())%3B
Related Issues:
The text was updated successfully, but these errors were encountered: