Skip to content

typechecking does not terminate on contrived recursive type #27073

Closed
@fictitious

Description

@fictitious

TypeScript Version: 3.1.0-dev.20180913

Search Terms:

typechecking infinite recursion

Code

export type DirectoryBase = {name: string}

export type DirectoryEntries = {readonly [n in string]: Directory};

export type Directory = DirectoryBase & {entries: DirectoryEntries};

export type ExactDirectory<E extends DirectoryEntries> = DirectoryBase & {entries: E};


export type Accessor<D extends Directory> = 
    D extends Directory ? {[K in keyof D['entries']]: Accessor<D['entries'][K]>} : {};


// intersection and conditional type are necessary to exhibit the problem
export type BadAccessor<D extends Directory> = {

} & (D extends Directory ? {[K in keyof D['entries']]: Accessor<D['entries'][K]>} : {});



function f<E extends DirectoryEntries, C extends Directory>(accessor: Accessor<C>): void {

    const a1 = accessor as Accessor<ExactDirectory<E>>; // ok, error as expected

    const a2 = accessor as BadAccessor<ExactDirectory<E>>; // boom
}

Expected behavior:

compilation error is reported

Actual behavior:

apparently, compilation does not terminate

Playground Link: nope WARNING pasting the above code to the playground freezes the playground UI, and because it remembers the code you are stuck forever. Browsers start warning about unresponsive page after a while.

Related Issues:
might be the same as #27066 but observed behavior is different

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions