You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Omitting a property from tuple breaks destructionΒ #54371
constfoo=[1,"2"]asconstconst[a,b]=foo// everything works well, a is number and b is stringconstbar=[1,"2"]asOmit<[number,string],"at">const[c,d]=bar// Oops! c and d are string | number now
That said, you still can't get rid of toString, because it's on Object itself. :) But you can get rid of Array properties, like forEach, as demonstrated in that Playground.
For the Omit case, the type hints differ depending on where you hover (declaration vs. use), whereas they're the same for a virgin tuple type. So there's something different happening with type inference between the two cases that could be causing issues in more complex scenarios.
Ray (@so1ve) That's just the same thing I already pointed out above: declaration site shows string | number, later uses have the proper type: Playground
changed the title [-]Omitting a property from tuple breaks the type[/-][+]Omitting a property from tuple breaks the type. Destruction does not work however index signature works[/+]on May 25, 2023
changed the title [-]Omitting a property from tuple breaks the type. Destruction does not work however index signature works[/-][+]Omitting a property from tuple breaks destruction[/+]on May 25, 2023
β¦And it's at this point in the writeup that I discover the actual problem (sort of). All you need to do to "fix" this is to change your target to ES2021 or earlier. This problem only appears to occur while targeting ES2022 and ESNext. I have no idea if that's intentional. My gut feeling is that it's a bug, because it seems to behave in ways that can't be right, but I suppose it's possible TS is just expressing something that's new in ES2022 that I'm not thinking of. (I've been surprised beforeβ¦)
Here's the issue reduced to something actually incorrect. Bizarrely, the type of the value changes depending on whether it's called as a function: when it's used in a function call, it picks of the union members, but it somehow picks the wrong one.
declareconstbar: Omit<[(x: number)=>void,(x: 1)=>void],"at">const[c,d]=bar// ^?// β This should be `(x: number) => void`c;// ^?// β This should also be `(x: number) => void`, and it should accept a 2c(2);// ^?d;// ^?
This problem only appears to occur while targeting ES2022 and ESNext. I have no idea if that's intentional. My gut feeling is that it's a bug, because it seems to behave in ways that can't be right, but I suppose it's possible TS is just expressing something that's new in ES2022 that I'm not thinking of.
This seems unlikely. Tuple types are purely a TS concept; they are plain old arrays at runtime. It seems more likely that something else changed in type inference that causes this indirectly.
I'm not quite sure how this should be adjusted though. Currently isArrayLikeType is defined like this:
functionisArrayLikeType(type: Type): boolean{// A type is array-like if it is a reference to the global Array or global ReadonlyArray type,// or if it is not the undefined or null type and if it is assignable to ReadonlyArray<any>returnisArrayType(type)||!(type.flags&TypeFlags.Nullable)&&isTypeAssignableTo(type,anyReadonlyArrayType);}
In general, tuples and arrays are not always handled in the compiler in various scenarios - one of other things that comes to my mind is that intersections with tuple/array members are not always handled as expected (although that works in this particular scenario). So perhaps a fix here should involve a more general overhaul of how those things are treated within the compiler instead of being an ad-hoc change to satisfy this particular scenario.
Activity
Peeja commentedon May 24, 2023
Here's a playground link for you.
so1ve commentedon May 24, 2023
Oh, thank you!
Peeja commentedon May 24, 2023
Hmm, that's not the behavior I'm seeing. I'm getting individual types for the members of the tuple.
That said, you still can't get rid of
toString, because it's onObjectitself. :) But you can get rid ofArrayproperties, likeforEach, as demonstrated in that Playground.so1ve commentedon May 24, 2023
Ah I think I'm providing a wrong reproduction. I'll update it later.
so1ve commentedon May 24, 2023
Could you please checkout https://github.com/so1ve/cride/blob/main/src/index.ts? Test the following code:
This works well when you destruct it like
const [a, b] = foo, but once you change the type to:It breaks.
so1ve commentedon May 24, 2023
weird, it still works...
fatcerberus commentedon May 24, 2023
Petra Jaros (@Peeja) One curious thing I noticed is this: https://www.typescriptlang.org/play?ts=5.0.4#code/PTAEBcHtIG3BLADgZ1AQwE4FMIAsfJoC2OAZpBqACZYDGM6AdlaAK7JYBQtkjy4ocpABMoALygA2gEYANKABEwhQF10qHn3Dde-KWmHyARsLUShwgNycQoO3YB6Afk72DNsM84eI0OElQqeFJSLEojLHAAdywsRmo6BjRmNg4dLUFoAGZxKTlFZTU0VAB5InhwAB4ZeSVVWvIMAFE0WlwFAD50vUk0LOMss0zILOtbe1AvNyyfLyA
For the
Omitcase, the type hints differ depending on where you hover (declaration vs. use), whereas they're the same for a virgin tuple type. So there's something different happening with type inference between the two cases that could be causing issues in more complex scenarios.so1ve commentedon May 24, 2023
Ah yes! That's a little bit like what I'm facing. Interesting that both my declaration and use are wrongly typed.
Peeja commentedon May 24, 2023
Bruce Pascoe (@fatcerberus) Huh, that is odd... π
so1ve commentedon May 25, 2023
so1ve commentedon May 25, 2023
It seems that mapping type will break the tuple type.
fatcerberus commentedon May 25, 2023
Ray (@so1ve) That's just the same thing I already pointed out above: declaration site shows
string | number, later uses have the proper type: Playgroundso1ve commentedon May 25, 2023
Link
In this example you can see that a and c is typed correctly but reject and resolve is not.
[-]Omitting a property from tuple breaks the type[/-][+]Omitting a property from tuple breaks the type. Destruction does not work however index signature works[/+][-]Omitting a property from tuple breaks the type. Destruction does not work however index signature works[/-][+]Omitting a property from tuple breaks destruction[/+]Peeja commentedon May 26, 2023
Found it! The issue seems to happen when one of the tuple type members is assignable to the other. For instance:
Instead of
1,dwas typed asnumber, which I would assume TS is reducing fromnumber | 1. It's easier to see when the elements are functions:And that kind of makes sense: here,
cis typed as accepting either anumberor specifically1; the latter type is just redundant. In fact, if you give them different return types so it's not redundant, you actually get the right type and not a union.β¦And it's at this point in the writeup that I discover the actual problem (sort of). All you need to do to "fix" this is to change your target to ES2021 or earlier. This problem only appears to occur while targeting ES2022 and ESNext. I have no idea if that's intentional. My gut feeling is that it's a bug, because it seems to behave in ways that can't be right, but I suppose it's possible TS is just expressing something that's new in ES2022 that I'm not thinking of. (I've been surprised beforeβ¦)
Here's the issue reduced to something actually incorrect. Bizarrely, the type of the value changes depending on whether it's called as a function: when it's used in a function call, it picks of the union members, but it somehow picks the wrong one.
fatcerberus commentedon May 26, 2023
This seems unlikely. Tuple types are purely a TS concept; they are plain old arrays at runtime. It seems more likely that something else changed in type inference that causes this indirectly.
RyanCavanaugh commentedon May 30, 2023
In user code, I'd recommend using a newer version of
Omitthat is homomorphic:But yeah the behavior describes seems a little off
Andarist commentedon Jun 7, 2023
This
isArrayLikeType(parentType)check is at fault here:https://github.dev/microsoft/TypeScript/blob/fbd63e9e43dc4e64184b590fa156002a7be6d47e/src/compiler/checker.ts#L10376
I'm not quite sure how this should be adjusted though. Currently
isArrayLikeTypeis defined like this:In general, tuples and arrays are not always handled in the compiler in various scenarios - one of other things that comes to my mind is that intersections with tuple/array members are not always handled as expected (although that works in this particular scenario). So perhaps a fix here should involve a more general overhaul of how those things are treated within the compiler instead of being an ad-hoc change to satisfy this particular scenario.