Skip to content

Regression in variance measurements for aliased recursive types: not falling back to structural checks when measuring #33872

Closed
@jack-williams

Description

@jack-williams

Version. 3.7-beta

Code

export interface CalcObj<O> {
    read: (origin: O) => CalcValue<O>;
}

export type CalcValue<O> = CalcObj<O>;

function foo<O>() {
    const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
    const x: CalcObj<O> = unk;
}

Expected behavior:

No error on the assignment to x.

Actual behavior:

Error. unknown is not assignable to O. The parameter O is being measured as invariant.

This works correctly on 3.6.3 and below. The usual tricks to avoid variance-based comparisons let the assignment pass.

Also, removing the type alias works too:

export interface CalcObj<O> {
    read: (origin: O) => CalcObj<O>;
}

function foo<O>() {
    const unk: CalcObj<unknown> = { read: (origin: unknown) => unk }
    const x: CalcObj<O> = unk; // fine!
}

Probably caused by #33050.

Playground Link: link

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions