-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Type logical operators "and", "or" and "not" in extends clauses for mapped types #31579
Comments
I don't think introducing more keywords and additional syntax for something that works well with existing tools already is worthwhile. |
Except it doesn't work well... It is hard to write, hard to read, error prone, and at the same time something quite fundamental, which projects will keep duplicating on and on. |
I've more or less switched to using After that the only thing missing is the abbreviated conditional type syntax |
@jcalz could you illustrate your ideas by applying them to the examples above? For instance, to I am curious, because I want to have such a feature today if this is possible, but it is most definitely a hack that I don't wish anybody to be forced to use for what would have otherwise been a very easy task. |
I suppose "hack" is in the eye of the beholder, here. While the boolean literal types On the other hand, The correspondence comes from equating a type to the statement that some value can be assigned to the type:
* well, if we get negated types Hopefully that seems less arbitrary. Of course, there is no So, let's look at your example: type Primitive = boolean | number | string | symbol | null | undefined | void
type IsA<T, E> = T extends E ? unknown : never;
type Cond<C, Y=unknown, N=never> = [C] extends [never] ? N : Y
type IsIndexSignature<P> = IsA<string, P> | IsA<number, P>
type Yes = IsIndexSignature<number> // (never | unknown) = unknown
type No = IsIndexSignature<boolean> // (never | never) = never Does that make more sense? |
Hmm... Sort of. I have tried to replace in a bigger example more or less every occurrence of The bottom line is, I do not believe your proposal is a reasonable one. Hack is not so much in the eye of the beholder, I would say. This arrangement of tricks is by any metric harder to understand than "regular binary logic" -- in my opinion, to the point where the pain is too high and making mistakes is significantly easier. |
If you mean this pattern: |
It's sly that TypeScript Handbook describes Conditional Types as
(from https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types) It never says Conditional Types will resolve to But not
|
Search Terms
Pretty much the ones in the title...
Suggestion
I would like the ability to use logical operators for types.
I am aware that
not
is already planned as a feature, but I would like to make sure that this gets further extended to other logical operators, and I couldn't find any hints that this is in your minds already.Use Cases
Type composition, better readability...
Examples
Current tricks to get to the desired behavior:
A few arbitrary use cases:
All together in the Playground: here
Desired syntactic sugar to write the same:
It would make the most sense to accompany this with better support for boolean checks in type definitions. That is, to allow to use the ternary operator directly without the constant need for
extends true
everywhere. Possibly, a new sort of "boolean type declaration" could be introduced, as to avoid having to propagate the boolean value all the way. For example, it should be possible to define KnownKeys (not full implementation here) like this:Without the need to do:
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: