microsoft / microsoft/TypeScript
Make type narrowing for destructured discriminated unions work for more types
Personne n'a encore pris cette issue.
- Langage dominant
- Go
- Étoiles
- 111k
- Forks
- 14.4k
- Merge moyen
- 1 j 19 h
- PR mergées (30 j)
- 117
Description
🔍 Search Terms
discriminant union, ref, control flow guard, type narrowing
✅ Viability Checklist
- This wouldn't be a breaking change in existing TypeScript/JavaScript code
- This wouldn't change the runtime behavior of existing JavaScript code
- This could be implemented without emitting different JS based on the types of the expressions
- This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
- This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
- This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals
⭐ Suggestion
type Ref<T> = { value: T }
type Data =
| { ready: Ref<true>, payload: string }
| { ready: Ref<false>, payload: null }
declare const data: Data
const { ready, payload } = data
if (ready.value) {
payload // <== currently inferred as "string | null" but should be "string"
}
Treat types like Ref<T> as a discriminant property in a union or find a way to narrow the type of payload
📃 Motivating Example
This is a very common use case in the Vue Pinia state store library, millions of projects use this library and have code like
const store = useDataStore()
const { ready, payload } = storeToRefs(store)
If we can improve this type narrowing behavior, the narrowed payload type can helps developer write safer code than before
// before, Non-null assertion everywhere
if (ready.value) {
payload.xxxx() // <=== false alert raised by typescript and developers have to use ?. or ! to avoid it
payload?.xxxx() // <=== ?. is unnecessary, generates dead code and brings cognitive confusion
xxxxx(payload!)
}
xxxxx(payload!) // <=== copied from the if block and forget to remove the ! mark, cannot receive alert from typescript
// after, everything works fine
if (ready.value) {
payload.xxxx()
xxxxx(payload)
}
xxxxx(payload) // received the null check protection from typescript
💻 Use Cases
The use cases is actually shown in the motivating example.
I've dig into the checker.ts for some time and here's my findings
getDiscriminantPropertyAccesscannot treatreadyas a discriminant property now because it needs to checkCheckFlags.Discriminantwhich impliesCheckFlags.HasLiteralType. It's a pretty strict check and as its name describes,Ref<T>has no chance to pass this check.- I'm not sure is it possible for relaxing the discriminate requirements but it seems to be a bad idea after some search. #29110 is what I found but it's a really old PR so maybe time changes now
- If we cannot solve it by using discriminant property narrowing, as a newbie to the typescript project, I just tried to debug the checker and have another idea
interface Ref<T> { value: T }
type ToRefs<T> = { [K in keyof T]: Ref<T[K]> }
function toRefs<T>(o: T): ToRefs<T> {
return {} as any
}
interface DataPrepared {
ready: true
payload: string
}
interface DataNotPrepared {
ready: false
payload: null
}
type Data = DataPrepared | DataNotPrepared
declare const data: Data
const { ready, payload } = toRefs(data)
function isDataReady(d: Data): d is DataPrepared {
return d.ready.value
}
if (isDataReady(data)) {
ready.value // <=== inferred as boolean but should be true
payload.value // <=== inferred as "string | null" but should be string
}
function assertDataReady(d: Data): asserts d is DataPrepared {}
if (ready.value) {
assertDataReady(data)
ready.value // <=== inferred as true which is expected but it's narrowed by other code path
payload.value // <=== inferred as "string | null" but should be string
}
Can we use type predicates or assert function to add more information to payload's flow list? If it's possible, maybe we can do following steps while examine the payload
- check
payload's symbol, if its declaration is aBindingPattern - check the flow list for
payload, if the narroweddatais the initializer ofpayload's declaration - narrow the
payloadbased on the narroweddata - maybe it's gibberish but hope it helps
Guide de contribution
Ouvrir le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Piste de recherche
Reproduisez l’exemple d’union discriminée déstructurée dans le playground TypeScript lié, puis lisez checker.ts autour de getDiscriminantPropertyAccess et de l’analyse de flux. Déterminez comment Ref pourrait participer au narrowing sans affaiblir les vérifications de discriminants existantes. Le travail est terminé lorsque l’exemple réduit payload à string lorsque ready.value vaut true, avec les tests appropriés ajoutés pour les cas pris en charge.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Évaluation
- Stack technique
- typescript
- Domaine
- compilers
- Type d'issue
- Fonctionnalité
- Difficulté
- 5/5
- Temps estimé
- Plus d'une semaine
- Activité
- À l'abandon
- Clarté
- Plutôt claire
- Accessibilité débutants
- 35/100