Closed
Description
TypeScript Version: 3.8.0-dev.20191121
Search Terms:
generic uses any instead of the default one when deconstructing a tuple
Code:
// lib
interface SelectProps<T, K> {
selector?: (obj: T) => K;
}
type SelectResult<T, K> = [K, T];
function select<T, K = T>(
obj: T,
props: SelectProps<T, K> = {},
): SelectResult<T, K> {
// default selector just returns the same object
const { selector = obj => obj } = props;
return [selector(obj), obj];
}
// usage
interface Person {
name: string;
surname: string;
}
function selectJohn<K = Person>(props?: SelectProps<Person, K>) {
const john: Person = {
name: 'John',
surname: 'Doe',
};
return select(john, props);
}
// 👍
// vvvvvvvv => correct type `Person`
const [person] = selectJohn();
// 👎
// vvvv => type `any`?
const [any, whatever] = selectJohn();
// 👍
const john = selectJohn();
// vvvvvvvvvvv => correct type again `Person`
const [personAgain, nufinspecial] = john;
Expected behavior:
Deconstructing a tuple uses the default generic type.
Actual behavior:
Deconstructing a tuple infers any
for when the default generic type should be used.
Playground Link: check the repro out here
Related Issues: Couldn't find any.