Skip to content

TS 4.1 regression in type assertion of generic mapped type #41617

Closed
@Igorbek

Description

@Igorbek

TypeScript Version: 4.1.2

Search Terms: CFA, narrowing, generic, mapped type, conditional type, type assertion

Code

interface A { x: number }

declare function isA(a: unknown): a is A;

type FunctionsObj<T> = {
  [K in keyof T]: () => unknown
}

function g<
  T extends FunctionsObj<T>,
  M extends keyof T
>(a2: ReturnType<T[M]>) {
  if (isA(a2)) {
    // a2 is not narrowed
    a2.x // error, but should be ok
  }
}

Expected behavior:

Type of a2 should be narrowed to ReturnType<T[M]> & A
It works this way in TS 4.0

Actual behavior:

Type of a2 remains only ReturnType<T[M]>

Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-beta#code/JYOwLgpgTgZghgYwgAgILIN7IB4C5kgCuAtgEbTIC+AUNQCYQIA2cUKMhICYwA9iMmABnVAAo4+TgGsQvAO4gAlPjiChaANy0wATwAOKAGKdufEEIDypAFYAeACoA+ZAF5M1ZMgDaAaUECpCB1eGGR7AF18UUVXZ2lZBWoaag4uHn5kAHNbDzDkCGxIEDp1YzSzSxsHRwAaXIBZfMKIYvVA4ND7akdxACZ8ACUIMEIoEHt9CAcvevDHGIxc4FDRYTE4XsUF3M8NgDpsZAB6I-yoKF4oXJoaIA

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptDomain: Conditional TypesThe issue relates to conditional typesFix AvailableA PR has been opened for this issueHas ReproThis issue has compiler-backed repros: https://aka.ms/ts-reprosRescheduledThis issue was previously scheduled to an earlier milestone

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions