microsoft / microsoft/TypeScript

Design Meeting Notes, 2026-08-27

Ouverte
#64,113 6 commentaires 0 réactions 0 personnes assignées Voir sur GitHub
Design Notes
Langage dominant
Go
Étoiles
111k
Forks
14.3k
Merge moyen
2 j 4 h
PR mergées (30 j)
132

Description

# Negated Types (`not T`)

https://github.com/microsoft/TypeScript/pull/63926

* If you think about a venn diagram of `A` and `B` overlapping where `A & B` is the overlap, `not A` is all the area outside of `A`, `not (A & B)` is the inverse color of that original overlap.
* Fresh object types are closed -
* Talked about this before, have a feeling it's more relevant today.
* Like what?
* Something like a `--noUnusedReturnedValues`
* Don't want to forget
* People use `void`, but this accepts `Promise`s that need to be `await`ed.
* Can have an `ignore` to help here.

```ts
function ignore(value: not PromiseLike): void {}
```
* `` `foo${string & not "reserved-key"}` ``
* `` Record<`on${string & not keyof EventNames}`, unknown> ``
* Can also do narrowing for carve outs of infinite sets.

```ts
function divide(a: number, b: number & not 0): number {
return a / b;
}

function doStuff(someValue: number) {
if (someValue !== 0) {
divide(10, x); // Okay

// (come back to this below)
const x = someValue;
divide(10, x); // ...
}
else {
divide(10, someValue); // Should error!
}
}
```
* Okay, but how does that work with aliases
* Currently an alias loses that information.
* That is super surprising.
* What are the old issues that this avoids?
* We had a prototype that swapped out our fact-tracking system in control flow analysis and replaced it with negated type tracking.
* But this was computationally intensive.
* This PR introduces negations only for specific types of control flow narrowing in the false branch where we needed to track that info.
* Only requests negation when a use-site of a binding needs negation.
* Done based on the contextual type at that use-site.
* We actually have similar logic here for when we narrow generic parameters in the body of a function!
* Very similar precedent here.
* Do you want the most precise type to be inferred
* e.g. type narrowing conditionals (come back to this?)
* We also infer type predicates for you now.
* Negated types seem useful over the set of primitives, and often works well enough in the object hierarchy, but doesn't really make sense to have a provable negated object type.
* An `object` doesn't necessarily exclude a `Promise` or a `Function`.
* But all sorts of checks like this are all heuristics-based.
* Could have a switch to check for negations?
* But is this a strictness thing?
* Feels like no - weird.
* Does this feel consistent?
* Should do this stuff everywhere.
* But if you have a switch case with 200 cases, you'll get 200 `not`s in the default, whether you cared or not. This PR avoids that unless you actually need the negation.
* Let's come back to use-cases.
* `not Promise`
* Control flow issues.
* With this PR, it is very nice that it's both faster and that you don't see the `not`s in hover unless you need.
* We do want to experiment a bit more with decomposing existing primitives here. There are some unknown unknowns here.
* Is `object` equivalent to `{} & not number & not string & not boolean & not symbol`?
* Is `{}` equivalent to `not null & not undefined`?
* Maybe `Extract` and `Exclude` can be defined in this way?
* Or rather, have the compiler produce intersections of negations wherever we otherwise produce these.
* Aside: added a specific mechanism for reducing intersections in the presence of negations.
* What does negation mean in the presence of optional properties?
* `not { optionalProp?: Type }`
* Out of time!

Guide de contribution

Ouvrir le guide de contribution

Piste de recherche

Commencez par lire les notes de réunion et la PR #63926 associée, en vous concentrant sur les types négatifs, le rétrécissement par le flux de contrôle, les alias et les propriétés optionnelles. Les notes contiennent des questions de conception ouvertes, mais ne nomment aucun fichier, test, point d’entrée ni critère d’achèvement ; il faudrait donc poursuivre la discussion et définir le périmètre avant l’implémentation.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Évaluation

Stack technique
typescript
Domaine
compilers, documentation
Type d'issue
Documentation
Difficulté
5/5
Temps estimé
Plus d'une semaine
Activité
À l'abandon
Clarté
À clarifier
Accessibilité débutants
25/100

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.