-
Notifications
You must be signed in to change notification settings - Fork 12.8k
strictNullChecks: Object property of class is wrongfully claimed to be possibly undefined #21853
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
Comments
This error is correct. The type of |
@RyanCavanaugh And if you have the time and its possible with TypeScript, how would I go about and map the type, so optional is removed? |
@jacobmadsen There's a long thread on the topic of removing optionality in #15012. The Meanwhile, an alternative strategy for your problem would be to start with types containing properties that are not optional and then add optionality as appropriate (since it is easy to do so). |
@ahejlsberg Thank you. I believe its enough to make a workable solution to my problem. |
Counter-argument: For example, with the new declare const x: { y?: number };
if ("y" in x) {
const y: number = x.y;
} but currently fails with Of course, the type of (rvalue) |
Is it the same issue? Why is // strictNullChecks: true
interface Car {
wheels?: number[];
}
const car1: Car = {}
car1.wheels = []; // set property outside of object literal
const numWheels1 = car1.wheels.length; // no error
const car2: Car = {
wheels: [],
}
const numWheels2 = car2.wheels.length; // Error: object is possibly undefined. |
Uh oh!
There was an error while loading. Please reload this page.
TypeScript Version: 2.8.0-dev.20180210
Search Terms: TS2532
Code
ts2532-test.ts:
tsc --strictNullChecks ts2532-test.ts
Expected behavior:
"propMap" is never undefined.
Actual behavior:
ts2532-test.ts(28,9): error TS2532: Object is possibly 'undefined'.
Playground Link: https://www.typescriptlang.org/play/#src=type%20SomeMap%3CT%3E%20%3D%20%7B%0D%0A%20%20%5BP%20in%20keyof%20T%5D%3A%20PropertyMap%3CT%2C%20T%5BP%5D%3E%0D%0A%20%20%7D%0D%0A%0D%0Atype%20SomeMapper%3CT%3E%20%3D%20(map%3A%20SomeMap%3CT%3E)%20%3D%3E%20void%0D%0A%0D%0Aexport%20default%20class%20PropertyMap%3CT%2C%20P%3E%20%7B%0D%0A%20%20asObject(map%3A%20SomeMapper%3CP%3E)%3A%20void%20%7B%0D%0A%20%20%20%20map(%7B%20street%3A%20%22street%22%20%7D%20as%20any)%0D%0A%20%20%7D%0D%0A%20%20asString()%3A%20void%20%7B%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20Mapper%3CT%20extends%20object%3E%20%7B%0D%0A%20%20constructor(map%3F%3A%20SomeMapper%3CT%3E)%20%7B%7D%0D%0A%7D%0D%0A%0D%0Aclass%20SomePerson%20%7B%0D%0A%20%20name%3F%3A%20string%0D%0A%20%20address%3F%3A%20%7B%20street%3F%3A%20string%20%7D%0D%0A%7D%0D%0A%0D%0Aclass%20SomePersonMapper%20extends%20Mapper%3CSomePerson%3E%20%7B%0D%0A%20%20constructor()%20%7B%0D%0A%20%20%20%20super(map%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20map.name.asString()%0D%0A%20%20%20%20%20%20map.address.asObject(propMap%20%3D%3E%20%7B%0D%0A%20%20%20%20%20%20%20%20propMap.street.asString()%20%2F%2F%20propMap%3A%20Error%3A(67%2C%209)%20TS2532%3A%20Object%20is%20possibly%20'undefined'.%0D%0A%20%20%20%20%20%20%7D)%0D%0A%20%20%20%20%7D)%0D%0A%20%20%7D%0D%0A%7D%0D%0A%0D%0Aconst%20mapper%20%3D%20new%20SomePersonMapper()
Related Issues:
The text was updated successfully, but these errors were encountered: