-
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
Possible to narrow type of object literal property with strictNullChecks? #20219
Comments
IMO I think it would be better if in let f2 = { bar: [] }; The type of |
@sandersn can you give this a shot and see if it's super hairy? The proposal is only to handle this (i.e. treat property assignments as assignments to properties) when a variable has a bare object literal initializer. IOW we are willing to Won't Fix this if it's going to be a ton of extra code / perf hit. |
I have a prototype in the branch Here are its other limitations:
|
Well, there's no significant change in performance, so I'll try cleaning up and hardening the change when I have time. |
I realised yesterday that none of our performance tests have |
Later: looks like there is still no performance impact, even with |
Fixed by #22006 |
@sandersn No update on this issue, the PR is closed due to inactivity, but the status is committed and the milestone is set to TypeScript 3.1 (after regularly bumped). |
Does this issue cover the below? The mention of object literal in the title makes me unsure, but I don't want to open a new issue if it's covered. Here, I have a function that takes a an object that has a property declared with Currently I have to force a cast with interface ThingWithString {
readonly value: string | undefined;
}
function printString(a: ThingWithString & {value: string}) {
console.log(a.value);
}
function getMeString(): ThingWithString {
return { value: "String" };
}
const maybeString = getMeString();
if (maybeString.value) {
printString(maybeString /*as ThingWithString & {value: string}*/);
} |
@DanTup no, you have a different problem, which is that Typescript will only narrow union types. In fact, there is no type |
@sandersn I see. Is there any safer way to do what I want than |
Looks like this might work... it's not perfect, but maybe better than the as (I don't know if there are perf implications, but for the places I'm doing this that's not a concern): if (maybeString.value) {
printString({ ...maybeString, value: maybeString.value });
} |
TypeScript Version: Current playground (2.6?)
Code
Expected behavior:
Both references to
.bar
compile successfully.Actual behavior:
Second reference (
f2.bar.push(0)
) fails with "object is possibly undefined" -- the type off2.bar
is still treated asnumber[] | undefined
even though it obviously can't be undefined.This might not be an actual bug as such but whatever magic allows the compiler to narrow the type of
f1.bar
after assigning to it, should also be able to narrow the type when it was assigned in the literal.The text was updated successfully, but these errors were encountered: