Skip to content

Unexpected intersection inferred from indexed type #35695

Description

@shigma

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

interfaceDataTypes{a: stringb: number}typeData={[KinkeyofDataTypes]: (value: DataTypes[K])=>void}functionmyFunc<KextendskeyofDataTypes>(data: Data,key: K,value: DataTypes[K]){data[key](value)}
Output
"use strict";functionmyFunc(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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions