microsoft / microsoft/TypeScript

Proposal: Allow isolated declarations to infer results of constructor calls

Open
#60,010 10 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Experimentation Needed Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

🔍 Search Terms

isolated declarations constructor generic infer

✅ Viability Checklist
⭐ Suggestion

Add a new flag or change the behavior of --isolatedDeclarations so that in the following code which is currently disallowed by isolated declarations:

export const foo = new Foo(a);

if the type of foo is not Foo, there is a typecheck error. In return, the type of foo will be emitted as Foo.

Similarly, if somehow in

export const foo: new Foo<A>(a);

foo is not a Foo<A>, there is a typecheck error.

📃 Motivating Example

Currently under isolated declarations, code must redundantly declare the type of a variable which is the result of a constructor call.

export const foo: Foo = new Foo(a);

This setting would also eliminate https://github.com/microsoft/TypeScript/issues/59768

💻 Use Cases

^

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points; start by tracing the existing --isolatedDeclarations checks and declaration inference behavior. Done means constructor-call results can be inferred under the proposed setting, with typechecking rejecting mismatched inferred or annotated types without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.