Skip to content

Generic conditional type X extends any ? X : X does not allow generic assignment of X #37272

Closed

Description

TypeScript Version: Nightly

Search Terms:
generic, conditional type, cannot be assigned, generic return value

Expected behavior:
A value of type X is a valid return value for a function returning Always<X>, since the latter is a generic conditional type that always evaluates to X.

Actual behavior:
Does not compile. Error message:
Type 'X' is not assignable to type 'Always<X>'. (2322)

Related Issues:
#31751 seems to be more about never type semantics.

Code
You can substitute any type for any in the definition below and it will fail regardless.

export type Always<X> = X extends any ? X : X
const f = <X>(): Always<X> => /*error*/ null as unknown as X /*error*/
Output
const f = () => null;
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

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

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