microsoft / microsoft/TypeScript
Generic arguments gets casted as "unknown" when used as function argument
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 1 d 19 h
- PR fusionados (30 d)
- 117
Descripción
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.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con el TypeScript Playground enlazado y reproduce el comportamiento relacionado con buildSchema, FormSchema e InferSharedStoreData. Rastrea por qué el tipo del parámetro de dataWithArg se convierte en InferSharedStoreData; se considera terminado cuando params se infiere como el StoreData derivado del store del esquema, mientras las demás propiedades siguen pasando la comprobación de tipos.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- compilers
- Tipo de issue
- Error
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 35/100