Skip to content
This repository was archived by the owner on Sep 1, 2026. It is now read-only.
This repository was archived by the owner on Sep 1, 2026. It is now read-only.

Repeated identical conditional return type triggers TS2719 ("Two different types with this name exist") under tsgo #3488

Closed

Description

@tomquist

Steps to reproduce

tsconfig.json:

{
  "compilerOptions": {
    "strict": true,
    "target": "ES2022",
    "module": "ESNext",
    "moduleResolution": "Bundler",
    "noEmit": true
  }
}

index.ts:

export function cast<T>(value: T): {
  as: <K extends T>() => null extends T ? K | null : undefined extends T ? K | undefined : K;
} {
  return {
    as: <K extends T>(): null extends T ? K | null : undefined extends T ? K | undefined : K => {
      return value as K;
    },
  };
}

Tested with @typescript/native-preview@beta (Version 7.0.0-dev.20260421.2) and [email protected].

Workaround: extract the conditional into a single named alias so both annotation sites reference the same type symbol.

type PreserveNullability<T, K extends T> = null extends T
  ? K | null
  : undefined extends T
    ? K | undefined
    : K;

export function cast<T>(value: T): {
  as: <K extends T>() => PreserveNullability<T, K>;
} {
  return {
    as: <K extends T>(): PreserveNullability<T, K> => {
      return value as PreserveNullability<T, K>;
    },
  };
}

Behavior with [email protected]

Compiles cleanly (exit 0, no diagnostics).

Behavior with tsgo

index.ts(5,5): error TS2719: Type '<K extends T>() => null extends T ? K | null : undefined extends T ? K | undefined : K' is not assignable to type '<K extends T>() => null extends T ? K | null : undefined extends T ? K | undefined : K'. Two different types with this name exist, but they are unrelated.
  Type 'null extends T ? K | (T & undefined) | (T & null) : undefined extends T ? K | (T & undefined) | (T & null) : K | (T & undefined) | (T & null)' is not assignable to type 'null extends T ? K | (T & null) : undefined extends T ? K | (T & undefined) : K'.
    Type 'K | (undefined extends T ? K | (T & undefined) | (T & null) : K | (T & undefined) | (T & null)) | (T & undefined) | (T & null)' is not assignable to type 'null extends T ? K | (T & null) : undefined extends T ? K | (T & undefined) : K'.
      Type 'undefined extends T ? K | (T & undefined) | (T & null) : K | (T & undefined) | (T & null)' is not assignable to type 'null extends T ? K | (T & null) : undefined extends T ? K | (T & undefined) : K'.
        Type 'K | (T & undefined) | (T & null)' is not assignable to type 'null extends T ? K | (T & null) : undefined extends T ? K | (T & undefined) : K'.
          Type 'T & undefined' is not assignable to type 'null extends T ? K | (T & null) : undefined extends T ? K | (T & undefined) : K'.
            Type 'K | (T & undefined) | (T & null)' is not assignable to type 'K | (T & null)'.
              Type 'T & undefined' is not assignable to type 'K | (T & null)'.
                Type 'T & undefined' is not assignable to type 'T & null'.
                  Type 'T & undefined' is not assignable to type 'null'.

The two types in the diagnostic are written out identically. Note also that in tsgo's elaboration the type has been re-distributed (K | (T & undefined) | (T & null)) compared with what the user wrote (K | null or K | undefined), suggesting the conditional is being reduced differently in the two positions before identity is checked.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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