microsoft / microsoft/TypeScript
Discriminated union with an optional discriminator is not being resolved as expected
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.3k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
🔎 Search Terms
discriminated union, discriminated union with an optional discriminator, type inference in objects
🕗 Version & Regression Information
I have a simple piece of code (see the playground/below)
The runtime flow is very simple: whatever gets returned by the loader func gets passed as data to the handle's crumbBuilder method. If the loader is absent then data will also be absent
But when it comes to typing those behaviors then 2 issues arise:
- At first, the discriminated union is not being resolved properly: in the second case it should be a string but TS cannot infer it and defaults to
any - At second, we still have to explicitly annotate or assert the
datatype, however from theloaderdefinition it is possible to infer its return type. Is there a syntax way to say 'the type of data is the whatever awaited type the loader function returns?. I have tried to achieve that behaviour using generics and thesatisifies` keyword but failed to
⏯ Playground Link
💻 Code
type PathSegment = object[];
type Handle<TData> = {
crumbBuilder: (data: TData) => PathSegment[];
}
type Loader<TData> = (args: { params: Record<string, string> }) => Promise<TData>;
type RouteHandler<TData = any> =
| {
handle: Handle<never>;
loader?: never;
}
| {
handle: Handle<TData>;
loader: Loader<TData>
};
const routeHandlerWithoutLoader = {
handle: {
crumbBuilder: (data) => [], //data is correctly inferred as never
}
} satisfies RouteHandler
const routeHandler = {
loader: async (args) => {
return args.params.userId;
},
handle: {
crumbBuilder: (data) => [] //data is not inferred as string
}
} satisfies RouteHandler<string>
🙁 Actual behavior
data is not inferred as string
🙂 Expected behavior
-
data should be inferred as a string
-
there is no need to explicitly annotate or assert the
datatype , and it must be inferred from the loader return type
Additional information about the issue
No response
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con l’esempio TypeScript Playground collegato e con le definizioni di RouteHandler, Handle e Loader nell’issue. Riproduci entrambi i casi, quindi verifica come il checker gestisce il discriminante opzionale e se i tipi restituiti dal loader possono propagarsi in handle. Il lavoro è completato quando, nei casi segnalati, data viene inferito come never senza un loader e come string con il loader, senza un’annotazione esplicita.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 5/5
- Tempo stimato
- Più di una settimana
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 25/100