Skip to content

Mapped type used with keyof not narrowing #36050

Closed
@whitneyit

Description

@whitneyit

TypeScript Version: Nightly

Search Terms:
computed type
mapped type narrow
keyof narrow type
type index narrow

Expected behavior:
No errors.

Actual behavior:
The type for value is not being narrowed on either side of the if statement.

Related Issues:

Code

interface Example {
    b: boolean;
    n: null;
}

function onlyBoolean(arg: boolean): void {
    // do nothing
}

function onlyNull(arg: null): void {
    // do nothing
}

function test<K extends keyof Example>(value: Example[K]): void {
    if (value === null) {
        // Argument of type 'Dict[K]' is not assignable to
        // parameter of type 'null'.
        onlyNull(value);
        return;
    }
        // Argument of type 'Dict[K]' is not assignable to
        // parameter of type 'boolean'.
    onlyBoolean(value);
}
Output
"use strict";
function onlyBoolean(arg) {
    // do nothing
}
function onlyNull(arg) {
    // do nothing
}
function test(value) {
    if (value === null) {
        // Argument of type 'Dict[K]' is not assignable to
        // parameter of type 'null'.
        onlyNull(value);
        return;
    }
    // Argument of type 'Dict[K]' is not assignable to
    // parameter of type 'boolean'.
    onlyBoolean(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

    Design LimitationConstraints of the existing architecture prevent this from being fixed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions