Repository navigation
Closed
Labels
BugA bug in TypeScriptA bug in TypeScriptEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisYou can do this
Milestone
Description
TypeScript Version: 3.6.0-dev.20190713
Search Terms: TS2339, union type object, union type property does not exist
And github suggestions while I was writing the title.
Code
type Test = {
someKey: false
} | {
someKey: {
someKeyInside?: number
},
someKeyThatExistOnlyIfOtherKeyIsNotFalse: string;
};
declare const test: Test;
if (typeof test.someKey === 'object') {
console.log(test.someKeyThatExistOnlyIfOtherKeyIsNotFalse);
}Expected behavior:
String in someKeyThatExistOnlyIfOtherKeyIsNotFalse should be logged
Actual behavior:
error TS2339: Property 'someKeyThatExistOnlyIfSomeKeyIsNotFalse' does not exist on type 'Test2'.
Property 'someKeyThatExistOnlyIfSomeKeyIsNotFalse' does not exist on type '{ someKey: false; }'.
29 console.log(test2.someKeyThatExistOnlyIfSomeKeyIsNotFalse); // TS2339: Property 'someKeyThatExistOnlyIfSomeKeyIsNotFalse' does not exist on type '{ someKey: false; }'.
Take a look at type Test1. The only one difference from type Test2 is someKeyInside should be always defined.
Related Issues:
Reacted by Jack Bates, Dan Vanderkam, GaoSong, Amin Ya, Kutner JS, bartenra and Edmundo Santos
Metadata
Metadata
Assignees
Labels
BugA bug in TypeScriptA bug in TypeScriptEffort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisYou can do this
Activity
[-]TS2339 Union type object bug[/-][+]Union not narrowed by `typeof x.y === 'object'` with mixed primitive and nonprimitive discriminants[/+]jack-williams commentedon Jul 18, 2019
I'm not sure
typeofhas ever triggered narrowing by discriminant.bterlson commentedon Jan 31, 2020
Just chiming in to say I would love this fixed! Our repro is something like the following:
Essentially we have some methods we'd like to return union of object types from. The object types are mostly distinct with a little overlap. Ideally users would just check the field that they want is present, and TS would do the math and present a proper type inside the
ifblock.We ran in to this because we were using a tagged union pattern which was working fine, but then attempted to generalize it, and found the narrowing stopped working.
[-]Union not narrowed by `typeof x.y === 'object'` with mixed primitive and nonprimitive discriminants[/-][+]Union not narrowed with `typeof x.y` as a discriminant[/+]ShuiRuTian commentedon May 27, 2020
Looks interesting. I would have a try on this.
ShuiRuTian commentedon Jun 2, 2020
Hi, if I define
What type of u and u.key should be?
Should it be Foo2? If so, could some guy give me some advice about how to change the flow?
jack-williams commentedon Jun 2, 2020
Maybe it should have comparable behavior to the literal type case?
danvk commentedon Jun 5, 2020
I ran across this after seeing Jack Williams (@jack-williams)'s comment here: #30622 (comment)
I was trying to "entangle" the refinements on two types by sticking them in an object together and was surprised that it didn't work:
(playground)
lazytype commentedon Aug 15, 2020
This would be useful to have since the improved tuple types in 4.0 will make overload implementations that use tuples even more common.
octogonz commentedon Aug 26, 2021
Is this a duplicate of #30506?
For its case, #30506 (comment) explained that:
Although this issue involves
typeof x.y, the principle seems pretty much the same. (?)xirzec commentedon Aug 26, 2021
The linked meeting notes about this (#36877) seem to suggest there is a possible solution (feature request):
Hopefully this is still feasible, and we don't have to close this issue as a "design limitation" like in #30506?
danvk commentedon Jan 24, 2024
Pulling in Ryan's comment from 2022 as the latest update on this issue #38839 (comment):
jakebailey commentedon Apr 9, 2024
This was fixed in #50344, as far as I can tell.