Closed
Description
TypeScript Version: 3.7.2
Search Terms: type alias index signature intersection TS2345
Expected behavior: pass the type checks
Actual behavior: fail with a TS2345 error
data[key]
// expected: (value: DataTypes[K]) => void
// actual: Data[K], with value: never
Related Issues: #31445, #35613
Note: Although #35613 is marked as duplicated, I think a different approach than #31445 can be used to address this issue. Actually data[key]
can be inferred as (value: DataTypes[K]) => void
, which solves the problem.
Code
interface DataTypes {
a: string
b: number
}
type Data = {
[K in keyof DataTypes]: (value: DataTypes[K]) => void
}
function myFunc <K extends keyof DataTypes> (data: Data, key: K, value: DataTypes[K]) {
data[key](value)
}
Output
"use strict";
function myFunc(data, key, value) {
data[key](value);
}
Compiler Options
{
"compilerOptions": {
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"useDefineForClassFields": false,
"alwaysStrict": true,
"allowUnreachableCode": false,
"allowUnusedLabels": false,
"downlevelIteration": false,
"noEmitHelpers": false,
"noLib": false,
"noStrictGenericChecks": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"esModuleInterop": true,
"preserveConstEnums": false,
"removeComments": false,
"skipLibCheck": false,
"checkJs": false,
"allowJs": false,
"declaration": true,
"experimentalDecorators": false,
"emitDecoratorMetadata": false,
"target": "ES2017",
"module": "ESNext"
}
}
Playground Link: Provided