Skip to content

Add API to get reduced typeΒ #63894

Description

πŸ” Search Terms

resolvedReducedType getReducedType

βœ… Viability Checklist

⭐ Suggestion

Please consider adding an API to get reduced type. Similar to getApparentType() or getContextualType(), it could be a getReducedType() method on the checker instance.

πŸ“ƒ Motivating Example

The following is a simplified reproduction of the problem:

declare const TaggedError: <Tag extends string>(
  tag: Tag,
) => new <A extends Record<string, any> = {}>(
  args: { readonly [P in keyof A]: A[P] },
) => { readonly _tag: Tag } & Readonly<A>;

class RateLimitError extends TaggedError("RateLimitError")<{
  readonly retryAfter: number;
}> {}

class QuotaExceededError extends TaggedError("QuotaExceededError")<{
  readonly limit: number;
}> {}

type Result1 = RateLimitError | (RateLimitError & QuotaExceededError);

type Result2 = RateLimitError | never;

Hovering (or using typeToString()) on Result1 and Result2 both give RateLimitError. Passing the same nodes to getTypeAtLocation() infers Result1 as a union and Result2 as plain RateLimitError.

The difference is caused by the NoTypeReduction type format flag. Enabling it in typeToString() would also return a union for Result1.

getTypeAtLocation() does not have options or flags, but similar functionality is implemented using getApparentType() or getContextualType() methods.

πŸ’» Use Cases

  1. What do you want to use this for?
  2. What shortcomings exist with current approaches?
  3. What workarounds are you using in the meantime?

I want to be able to get reduced type programmatically. The underlying algorithm is rather complex and seems to be hard to implement on the user side. I can’t figure out any workarounds, but it also might be that I missed something.

Metadata

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