Skip to content

Values are not assignable to deferred conditional types even when they are assignable to both branches. #41009

Closed
@s0

Description

@s0

TypeScript Version: 4.1.0-dev.20201009 & 4.0.3

Search Terms: deferred conditional assignment not assignable to type

Code

const doSomething = <T extends boolean>(t: T) => {
  const a: string = 'foo';
  const b: string | undefined = 'bar';
  const c: T extends true ? string : (string | undefined) = 'baz' as string;
  return c;
}

Expected behavior:

(baz as string) is assignable to c as string is assignable to both branches of the deferred conditional type.

Actual behavior:

The following error is emitted:

Type 'string' is not assignable to type 'T extends true ? string : string | undefined'.ts(2322)

Playground Link: https://www.typescriptlang.org/play?ts=4.1.0-dev.20201009#code/FAYw9gdgzgLgBAEzAZTAWwKYwBYEsIDmcAvHADwAqcGAHjBhAlHAEZhgA2GAhhAHwAKGAC44FAJQk+cAN7A4ccNHjdRsAE74ipAOQAzdjoDc8xZFis1MTYTgAfOAFdGGPfgwIScHS27rjpkoWIKJUtPSMzNaOGHAA-HAaWnCiAkm2Ds4Iru4Ikrq+AF4BCupYjuoQiiYAvkA

Related Issues: #39364 #39577

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions