microsoft / microsoft/TypeScript
Generic arguments gets casted as "unknown" when used as function argument
Nessuno ha ancora preso questa issue.
- Lingua principale
- Go
- Stelle
- 111k
- Fork
- 14.4k
- Merge medio
- 1g 19h
- PR unite (30g)
- 117
Descrizione
Typescript version : 5.0.3 (But found on all versions I tested)
Search terms : generic type as function argument unknown
⏯ Playground Link
Playground link with relevant code
💻 Code
Here are the two main generics involved in my case :
type FormSchema<
StoreKey extends string,
StoreData extends Record<string, unknown>
> = {
store: { key: StoreKey; value: () => unknown }[];
fields: Array<{
key: string;
data: StoreData;
dataWithoutArg: () => StoreData;
dataWithArg: (params: StoreData) => StoreData;
}>;
};
type InferSharedStoreData<Store extends { key: string; value: () => unknown }[]> = {
[K in Store[number] as K["key"]]: Awaited<ReturnType<K["value"]>>;
}
And here's the actual function that casts these :
function buildSchema<
TSchema extends FormSchema<StoreKey, StoreData>,
StoreKey extends string,
StoreData extends Record<string, unknown> = InferSharedStoreData<
TSchema["store"]
>
>(schema: TSchema) {
return schema as TSchema
}
🙁 Actual behavior
The "params" argument of the "dataWithArg" sub-property of "fields" is being typed as InferSharedStoreData, which means the "store" property type of the schema passed as argument of buildSchema is unavailable in the context of the function argument.
the StoreData type is being properly inferred for every other property it's assigned to, except on the case where it is used as a function argument.
🙂 Expected behavior
The function argument "params" should be inferred as StoreData, which itself is InferSharedStoreData<schema['store']>. A complete example is set-up on the playground link above.
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 il TypeScript Playground collegato e riproduci il comportamento relativo a buildSchema, FormSchema e InferSharedStoreData. Traccia il motivo per cui il tipo del parametro di dataWithArg diventa InferSharedStoreData; il lavoro è completato quando params viene inferito come lo StoreData derivato dallo store dello schema, mentre le altre proprietà continuano a essere verificate dal sistema dei tipi.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- typescript
- Ambito
- compilers
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100