microsoft / microsoft/TypeScript
Wrong overload selected on function when passed a callback annotated with a type with multiple optional properties
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- Go
- Estrellas
- 111k
- Forks
- 14.3k
- Merge medio
- 2 d 4 h
- PR fusionados (30 d)
- 132
Descripción
TypeScript Version: 2.7.0-dev.20171128
Code
This issue was first noticed in code that uses LoDash with the typings from DefinitelyTyped. It seems to manifest only in a very specific set of circumstances. The following code sample is as minimal a reproduction as I could find.
let _: LoDashStatic;
interface LoDashStatic {
mapValues<T extends object, TResult>(
obj: T,
callback: (value: T[keyof T]) => TResult
): { [P in keyof T]: TResult };
mapValues<T extends object>(obj: T, iteratee: object): { [P in keyof T]: boolean };
}
interface Item {
key?: string;
bar?: number;
}
interface Collection {
[key: string]: Item;
}
const source = { foo: { bar: 42 } };
let items: Collection = _.mapValues(
source,
(v: Item) => v
);
Note that the correct overload is selected when any of the following changes are made to the above code:
- Remove the type annotation on the callback:
(v) => vinstead of(v: Item) => v - Remove the nested optional property:
{ foo: {} }instead of{ foo: { bar: 42 } } - Remove the unused optional property:
interface Item { bar?: number; }instead ofinterface Item { key?: string; bar?: number; } - Annotate the source collection:
const source: Collection = { foo: { bar: 42 } };instead ofconst source = { foo: { bar: 42 } };
Expected behavior:
Compiles without issue.
Actual behavior:
error TS2322: Type '{ foo: boolean; }' is not assignable to type 'Collection'.
Property 'foo' is incompatible with index signature.
Type 'boolean' is not assignable to type 'Item'.
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 la reproducción mínima en TypeScript del issue e investiga la resolución de sobrecargas para el callback anotado, las propiedades opcionales y el tipo de origen inferido. Se considera terminado cuando el ejemplo compila sin el error TS2322 y selecciona la sobrecarga de callback en lugar de la sobrecarga de object-iteratee.
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
- 42/100