microsoft / microsoft/TypeScript

Generic arguments gets casted as "unknown" when used as function argument

Ouverte
#53,695 2 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Personne n'a encore pris cette issue.

Needs More Info
Langage dominant
Go
Étoiles
111k
Forks
14.4k
Merge moyen
1 j 19 h
PR mergées (30 j)
117

Description

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.

Guide de contribution

Ouvrir le guide de contribution

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Piste de recherche

Commencez par le TypeScript Playground lié et reproduisez le comportement autour de buildSchema, FormSchema et InferSharedStoreData. Suivez pourquoi le type du paramètre de dataWithArg devient InferSharedStoreData ; le travail est terminé lorsque params est inféré comme le StoreData dérivé du store du schéma, tandis que les autres propriétés continuent d’être vérifiées par le système de types.

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

Évaluation

Stack technique
typescript
Domaine
compilers
Type d'issue
Bug
Difficulté
4/5
Temps estimé
3-5 jours
Activité
À l'abandon
Clarté
Plutôt claire
Accessibilité débutants
35/100

Recevez les nouvelles issues par e-mail

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