-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Improved excess property checks in union types / new paradigm: use literal type infered from literal initializer #42997
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 sounds a lot like #7481 - comments? |
One takeaway from issue #7481 is that it proposes an
Further down, it is suggested that
These are related to the use case I meant to propose, but even if The use case I am concerned with is an in-place strict-union operation to allow for deep excess property checking. I am calling it an 'in-place deep strict-union operation' and not a 'strict-union type defintion` to make clear it is not a repeat of the request for 'exact types'. I have read the comment on why exact types conflict with the existing definition of types, in particular
and agree completely. How this differs is that it requires no change to Typescript defintion of types. In fact the in-place deep strict-union is already possible(*) using type functions such as distributed conditional type operations. (*Possible except for the fact that that an 'possibly infinite depth error' is falsely emitted, and the type function silently stops working when the number of data reaches a certain size .) Let me demonstate:
Note1: definitions for Note2: In this typical use case an array of testData is populated with items The definition for
The point of this present issue is to suggest that Looking at issue #7481 inspires the following idea:
However, that emits the following errors:
The "working" (excepting problems noted above) code for the strict-union function |
|
I think that modifying the typescript distributed-types meta-language to be more robust is probably not a practical suggestion. A better way would be to utilize the typescript API or other typescript utilities to create a program that allows customized behavior layered over typescript. That is how linting works - decoupled. Unlike linting, however, deep excess property checking would require the symbols from compilation. The typescript API provides such an interface. That would enabled development decoupled from the main typescript release. It would be the best way to progress. Therefore I will close this issue, as the design of such decoupled downstream development is not material for an issue. Incidentally, I have submitted another issue #44871, part of which requests clarification on the actual intended rules for excess type checking. Because hard specification for such rules (even if they may change in the future) comprise an interface, and therefore are a requirement for decoupled downstream development, 44871 is an issue that I hope can be resolved. |
Suggestion
🔍 Search Terms
List of keywords you searched for before creating this issue. Write them down here so that others can find this suggestion more easily and help provide feedback.
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Consider the statement:
The last line is an assignment statement. The processing taking place in the assignment has two distinct and separate parts:
SomeTypeDef
with the single input variableTU
.{<some props>}
to the l.h.s type. That computation takes place using Typescript's fixed assignment rules that cannot be changed.Now suppose we define an additional type
which you will notice is the type of the literal initializer on the r.h.s. of the assignment. Now suppose we add that type as an additional variable to a type function on the l.h.s.:
Now the l.h.s type function has additional information to work with . Therefore whatever
SomeTypeDef<TU>
can express,SomeTypeDefPlus<TU,I>
can also express in the same length code. HoweverSomeTypeDefPlus<TU,I>
may express more things thanSomeTypeDef<TU>
, and/or may be able to express the same things in shorter code. In psuedo-psuedo code:Unfortunately
type I = {<some props>}
, AND.... = {<some props>}
is twice as much writing - a code length penalty.
The proposal is to enable the type of the r.h.s. literal initializer to be inferred, e.g.,
or any other syntax that would automatically set the value of
I
on the l.h.s. to betype I = {a:1,b:2}
Is there really any need for a
SomeTypeDefPlus<TU,I>
functionality? Yes. motivating function below explains thatit makes it easier to write a generalized function for strict excess property checking in type unions .
📃 Motivating Example
Typescript Playground
Two criteria by which to compare
ExclusifyUnion
andExclusifyUnionPlus
-As for 'ease and clarity' ,
ExclusifyUnionPlus
does seems easier to code and comprehend.As for 'total range of expression', I am not sure. It would require a proof.
💻 Use Cases
The proposed syntax
or something like it would simplify the leveraging existing functionality for the purposes of strictly detecting excess properties in literal assignments to union of Record types. That includes deep Record types with deep member which are themselves unions of Record types.
It could also have other uses besides detecting excess properties.
Reference
This suggestion is based on an answer I posted on SE to a question about excess property checks.
The text was updated successfully, but these errors were encountered: