-
Notifications
You must be signed in to change notification settings - Fork 12.8k
exactOptionsPropertyTypes - anomolous behavior with Tuples, options, and spreading. #45764
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 is the first time I ever heard of this flag. It's not mentioned in the 4.4 release notes. Where did you find this? |
@MartinJohns - Despite the name, it apparently applies to Tuples as well. From the link given in the 4.4 documentation to the related pull
It appears the name changed from |
@andrewbranch thank you |
FWIW, that JSON dump of type info is not from our compiler API. I’ve never heard of properties like |
@andrewbranch - Sure - I'll take it out. Should I remove that whole final section "Tangent topic yet critical - Type API may have changed - is it still possible to determine behavior from exposed Type API ?" It's not third party API or a compiler. It's from code I wrote displaying the information obtained via (... Tangential information describing motivation for using compiler API's That final section of my post is about that compiler API - perhaps - no longer enough info to predict what will pass the compiler in some instances (optional vs. explicit undefined). |
@andrewbranch - Here is a link to the rawish output. The In the out you see some places where |
Yeah, it would be better if an API request were split into a separate issue please. And it would be easier for me to evaluate without it being intercepted by “a generalized type explorer, which is a component of a typescript Type to schema translator.” If you’re not using the compiler API directly, I can’t tell whether the compiler API is actually deficient. |
@andrewbranch Removed.
But I think that is not the Compiler API you mean right now. You mean using the compiler API exposed either through |
No, I’m familiar with the full checker API, and I’m sympathetic to ensuring it’s usable, it just doesn’t seem relevant to this issue and I want to evaluate it without it having been serialized into a different structure with different property names by some mysterious 3rd party tool. |
@andrewbranch - You are absolutely correct that it is a separate issue and I think my judgement was poor. (I've already figure how to solve that separate API issue so don't worry). About the problem at hand I made some more examples around the strange result.
Out of T2a,T2b, T2, only T2 is failing.The difference with T2 is that it has an element after the variadic. The ones that work correctly (T2a,T2b) (and T4 and T4eq) all make unions at the highest level. |
The OP claims that the type produced by this spread is incorrect: type T1 = [x: number, x?: boolean];
type T2 = ["^", ...T1, "$"]; // ["^", number, boolean | undefined, "$"] The latter examples imply that the expected behavior would be to produce When spreading a tuple without adding extra trailing elements the original optional flag is preserved (and not expanded into |
Yeah, there are a couple things wrapped up into one here
The first one is effectively a feature request, but not one we'd be likely to pick up since I think it'd have serious combinatorial explosion problems. Re second one, the current behavior is maybe the best approximation of what we could do. I think there's an argument to be made that under EOPT, tuple spreads should behave as if you had written type T2 = ['^',...Required<T1>,'$'];
const t21:T2 = ['^',1,true,'$'];
const t22:T2 = ['^',1,undefined,'$']; // should be + is an error Though if you want that behavior, you can write that today, but if you like today's behavior more and we switch, there wouldn't be a way to go back except to turn off EOPT -- but maybe that's what EOPT means, after all. |
Having just read what I wrote, I'm maybe unsure now, since it's not like |
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
Spreading of Variadic Tuple types, as well as the ability to indicate final position optional parameters with labels, were introduced in ts 4.0. The noted behavior has been been present since then.
However, although there was hope it would be fixed with 4.4's exact
exactOptionsParameters
, it was not.⏯ Playground Link
https://www.typescriptlang.org/play?exactOptionalPropertyTypes=true&ts=4.4.2#code/ATAuE8AcFNgFQIzALzANoA8BcA7ArgLYBG0ATgDTAYD8WRA9vQDbQCGOAugFAgDG9OAM6gwCBFkQp0CcnhwATaADMAljmjyOwAPTbgAUQAaAYX0AZM-oBycAIS3gAdxWgAFsGgZWvUAHlIoCoCrEwACqT0MKQQcFDQgligpHiwbiqCwCS8rASw6cDsHqQRpAB0wDzA-EIioAgATBJIqGgySSncfALCogDMTVKt3JUQMPD1gwDkAHqT5KULiOSTACSTnVXdtfXicBMtM3NtydDLaxwA3JXVPaD1jXtTs+Qycoqq6vJn6xc6ejj0MikYAAWmAxjMAEEAJIAWVBYFc+QAygAJXwAVTMABFMrBCkD6KRrlswPV+o8Ds8ZKsfn8isCwRCYfCwWkMmjMTjgFZfHA8QUcAyiRUgA
💻 Code
🙁 Actual behavior
In the above code, variable declaration for
t22
is NOT an err.In the above code, variable declaration for
t23
IS an err.🙂 Expected behavior
In the above code, variable declaration for
t22
IS an err.In the above code, variable declaration for
t23
is NOT an err.Even though with
exactOptionalPropertyTypes
, theT1
now no longer considers[number,undefined]
to be a legal assignment, whenT1
is spread intoT2
the old behavior is used instead of the new behavior.The documentation for exactOptionalPropertyTypes says
The text was updated successfully, but these errors were encountered: