Skip to content

Cannot be used to index type error for generic conditional typeΒ #50465

Closed
@pkudryashov

Description

@pkudryashov

Bug Report

πŸ”Ž Search Terms

Generic conditional type index

πŸ•— Version & Regression Information

  • This is a regression
  • This changed between versions 4.7.4 and 4.8.2

⏯ Playground Link

type Column<T = unknown> = (keyof T extends never ? { id?: number | string } : { id: T }) & {
    title?: string;
}

// no error if `key: keyof Column`
function getColumnProperty<T>(column: Column<T>, key: keyof Column<T>) {
    return column[key];
}
Output
"use strict";
// no error if `key: keyof Column`
function getColumnProperty(column, key) {
    return column[key];
}
Compiler Options
{
  "compilerOptions": {
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "alwaysStrict": true,
    "esModuleInterop": true,
    "declaration": true,
    "experimentalDecorators": true,
    "emitDecoratorMetadata": true,
    "target": "ES2017",
    "jsx": "react",
    "module": "ESNext",
    "moduleResolution": "node"
  }
}

Playground Link: Provided

πŸ™ Actual behavior

Error:" Type '"title" | keyof (keyof T extends never ? { id?: string | number | undefined; } : { id: T; })' cannot be used to index type '({ id?: string | number | undefined; } | { id: T; }) & { title?: string | undefined; }'.

πŸ™‚ Expected behavior

No error

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions