-
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
Automatic type guard #51409
Comments
This violates the design goals of TS, in particular these two checkboxes in the issue template:
That TypeScript types are 100% invisible at runtime is by design. |
@fatcerberus Thanks for reply! I understand. But is there any reason why types cannot be "type guarded automagically" that makes this rule inviolable? I mean, I understand it's good to have rules, and I really imagine there's a reason, but this exception could make Typescript smarter by allowing And, in this case, there would really be a need to have a JS code elaboration during the process by the TS. But how much is this a problem that cannot be treated? Are there practical reasons for this, for example? For example, I would understand that complex cases TS could not allow the use of |
@MartinJohns Yes, but that's exactly what I'd like to suggest. As Another option would be to make this clearer, for example allowing the use of Or creating a new operator, for example Or as a method of |
It's against TypeScripts Language Design Goals and was rejected several times already. |
Per @RyanCavanaugh, the TS dev lead:
|
Suggestion
π Search Terms
automatic type guard, smart type guard
β Viability Checklist
My suggestion meets these guidelines:
β Suggestion
Typescript can perform type narrowing in certain circumstances intelligently (and every day I am more surprised). However,
type
s are not JS compatible, so something likeinstanceof SomeType
is not allowed, as during conversion thetype
is lost.In this case, we can perform a type guard manually, so that we can determine that the information we are receiving is compatible with the expected type.
For example (Playground):
This will work perfectly. But suppose at some point I say that
Vehicle
will now haveage
andAnimal
will havebreed
.In this case, our
isAnimal()
type guard will incorrectly interpret that a vehicle is an animal. Because the type guard works on top of information that is now common to both.To solve the problem, we can change the way
isAnimal()
works, making it work by checking if it hasbreed
instead:However, whenever something similar happens, we need to be careful to make sure that our type guard conforms to the uniqueness of the type, while not conflicting with another possible similar type (not shown here).
So my suggestion is that Typescript could actually use
item instanceof <Type>
, and it could intelligently infer (whenever possible) a type guard for the expected type.In this case, even though
Animal
is atype
(which in principle is impossible nowadays), Typescript would automatically check how it could safely type narrow inside this function, based on the type of data ofAnimal
in relation to the unionAnimal | Vehicle
.So in our first example, the code would convert to something like:
And if the signature of
Animal
andVehicle
changes, as in the second example, the generated code would automatically change to ensure that the function has the same behavior:Note: in this
else if()
we can use'brand' in item
or'age' in item
because althoughage
belongs to both types,if()
has already captured theAnimal
type.The text was updated successfully, but these errors were encountered: