Skip to content

Inconsistent recursive type allowed by Typescript #38098

Closed

Description

TypeScript Version:

3.8.3

Search Terms:

Recursive type, recursive type alias, inconsistent type

Code

type M<T> = { value: T };
interface M2 extends M<M3> {};  // M2 = { value: M3 }
type M3 = M2[keyof M2];         // M3 = M3

function f(m: M3) {
  return m.value;
}

Expected behavior:

Typescript should either show an error message on M3 signalling the inconsistent type or consistently infer M3 to be never.

Actual behavior:

Typescript accepts M3 as a valid type. When I hover over the parameter type of m in the function it infers type M3 = any, while when used as a value (e.g. m.value it infers it as never).

Playground Link:

https://www.typescriptlang.org/play/?ssl=8&ssc=1&pln=1&pc=1#code/C4TwDgpgBAsgPAFQHxQLxQN5QG4EMA2ArhAFxQJQC+A3ALABQAlgHbAQBOAZrgMbQwAmKBAAebZgBMAzrDgwAzCgw0oUAPRrYQ9FjxFSseVQahIhtFoDaAawggA9py0BdaqvceN59AoYNOhMw8wIz2zFCcABQAtmQKAJSYDKrsEMCE7OHRAHR6xHT0lAxAA

Related Issues:

None

Metadata

Metadata

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