TypeScript: Regression when using indexed type depending on function argument as return type
TypeScript Version: 3.5.1
Search Terms:
Code
interface Foo {
	prop1: { value: number }[];
	prop2: string[];
}
function getFoo<T extends keyof Foo>(key: T): Foo[T] | undefined {
	if (key === "prop1") {
		return [{ value: 1 }]; // Error here
	}
}
const bar = getFoo("prop1"); // has correct type
Expected behavior: No error. This used to work in 3.4.5
Actual behavior:
Type '{ value: number; }[]' is not assignable to type 'Foo[T]'.
  Type '{ value: number; }[]' is not assignable to type '{ value: number; }[] & string[]'.
    Type '{ value: number; }[]' is not assignable to type 'string[]'.
      Type '{ value: number; }' is not assignable to type 'string'.
About this issue
- Original URL
 - State: closed
 - Created 5 years ago
 - Comments: 24 (10 by maintainers)
 
@RyanCavanaugh Don’t you think cases like this should work though? Is this unsound?
This wasn’t properly checked in 3.4: