Repository: TypeScript
Issue: Type logical operators "and", "or" and "not" in extends clauses for mapped types
Labels: Suggestion,Awaiting More Feedback

## 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](https://github.com/Microsoft/TypeScript/pull/29317), 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:

```ts
type Not<T extends boolean> = T extends true ? false : true

type Or<A extends boolean, B extends boolean> = A extends true
    ? true
    : B extends true
        ? true
        : false

type Or3<A extends boolean, B extends boolean, C extends boolean> = Or<A, Or<B, C>>

type And<A extends boolean, B extends boolean> = A extends true
    ? B extends true
        ? true
        : false
    : false
```

A few arbitrary use cases:

```ts
type Primitive = boolean | number | string | symbol | null | undefined | void
type IsA<T, E> = T extends E ? true : false

type IsIndexSignature<P> = Or<IsA<string, P>, IsA<number, P>>

type IsCallback<F extends Function> = F extends (...args: any[]) => any
    ? And<Not<IsA<Parameters<F>[0], Primitive>>, IsA<Parameters<F>[0], Event>> extends true
        ? true
        : false
    : false
```

All together in the Playground: [here](https://www.typescriptlang.org/play/#src=type%20Not<T%20extends%20boolean>%20%3D%20T%20extends%20true%20%3F%20false%20%3A%20true%0D%0A%0D%0Atype%20Or<A%20extends%20boolean%2C%20B%20extends%20boolean>%20%3D%20A%20extends%20true%0D%0A%20%20%20%20%3F%20true%0D%0A%20%20%20%20%3A%20B%20extends%20true%0D%0A%20%20%20%20%20%20%20%20%3F%20true%0D%0A%20%20%20%20%20%20%20%20%3A%20false%0D%0A%0D%0Atype%20Or3<A%20extends%20boolean%2C%20B%20extends%20boolean%2C%20C%20extends%20boolean>%20%3D%20Or<A%2C%20Or<B%2C%20C>>%0D%0A%0D%0Atype%20And<A%20extends%20boolean%2C%20B%20extends%20boolean>%20%3D%20A%20extends%20true%0D%0A%20%20%20%20%3F%20B%20extends%20true%0D%0A%20%20%20%20%20%20%20%20%3F%20true%0D%0A%20%20%20%20%20%20%20%20%3A%20false%0D%0A%20%20%20%20%3A%20false%0D%0A%0D%0Atype%20Primitive%20%3D%20boolean%20%7C%20number%20%7C%20string%20%7C%20symbol%20%7C%20null%20%7C%20undefined%20%7C%20void%0D%0Atype%20IsA<T%2C%20E>%20%3D%20T%20extends%20E%20%3F%20true%20%3A%20false%0D%0A%0D%0Atype%20IsIndexSignature<P>%20%3D%20Or<IsA<string%2C%20P>%2C%20IsA<number%2C%20P>>%0D%0A%0D%0Atype%20IsCallback<F%20extends%20Function>%20%3D%20F%20extends%20(...args%3A%20any%5B%5D)%20%3D>%20any%0D%0A%20%20%20%20%3F%20And<Not<IsA<Parameters<F>%5B0%5D%2C%20Primitive>>%2C%20IsA<Parameters<F>%5B0%5D%2C%20Event>>%20extends%20true%0D%0A%20%20%20%20%20%20%20%20%3F%20true%0D%0A%20%20%20%20%20%20%20%20%3A%20false%0D%0A%20%20%20%20%3A%20false%0D%0A)

Desired syntactic sugar to write the same:

```ts
type IsIndexSignature<P> = IsA<string, P> or IsA<number, P>

type IsCallback<F extends Function> = F extends (...args: any[]) => any
    ? not IsA<Parameters<F>[0], Primitive> and IsA<Parameters<F>[0], Event> extends true
        ? true
        : false
    : false
```

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](https://stackoverflow.com/questions/51465182/typescript-remove-index-signature-using-mapped-types) (not full implementation here) like this:

```ts
type KnownKeys<T> = {
    [P in keyof T]: IsIndexSignature<P> ? never : T[P]
}
```

Without the need to do:
```ts
type KnownKeys<T> = {
    [P in keyof T]: IsIndexSignature<P> extends true ? never : T[P]
}
```

## Checklist

My suggestion meets these guidelines:

* [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
* [x] This wouldn't change the runtime behavior of existing JavaScript code
* [x] This could be implemented without emitting different JS based on the types of the expressions
* [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
* [x] This feature would agree with the rest of [TypeScript's Design Goals](https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals).