Skip to content

[API] ConditionalType has no counterpart to root of TypeScript 6 (the node it was declared with) #64740

Open

Description

@MARCROCK22

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

In TypeScript 6 a conditional type has root: ConditionalRoot (public in typescript.d.ts), and root.node is the ConditionalTypeNode it was declared with. In the TypeScript 7 API, ConditionalType has getCheckType(), getExtendsType(), getTrueType() and getFalseType(), and nothing that leads to that node. For an instantiated conditional type without an alias symbol I have found no way to get its branches as they were declared.

declare class Box<T, V> {
  get(): T extends string ? V : never;
}
export function h<U>(box: Box<U, { a: number }>) {
  return box.get();
}

For the type of box.get(), which prints as U extends string ? { a: number; } : never in both versions:

TypeScript 6.0.3 TypeScript 7.0.2 (typescript/unstable/sync)
true branch as declared checker.getTypeFromTypeNode(type.root.node.trueType) is V not reachable
true branch of this instantiation type.getTrueType() is { a: number; }
type.aliasSymbol undefined undefined

On main (aa81492), tsc/internal/api/proto.go still sends only checkType and extendsType for a conditional type, and the methods are getTrueTypeOfConditionalType and getFalseTypeOfConditionalType, so I believe this is the same in 7.1.

Use case. typescript-eslint's isTypeReadonly (behind prefer-readonly-parameter-types) looks at the declared branches:

[type.root.node.trueType, type.root.node.falseType].map(checker.getTypeFromTypeNode)

With TypeScript 6 it answers true for the type above (V and never are readonly). A port to the new API can only look at the instantiated branches, and { a: number } is not readonly, so it would answer false.

Request. A way to get the declaration node of a conditional type, for example ConditionalType.getRoot() with node and isDistributive as in TypeScript 6. #64533 asks for getInferTypeParameters(), which lived on the same root object.

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

Metadata

Metadata

Labels

Needs Investigation

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions