microsoft / microsoft/TypeScript

[ID-Prep] Preserve type nodes from function expressions in initialisers

Open
#57,677 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

🔍 Search Terms

declaration emit preserve types from functions

✅ Viability Checklist
⭐ Suggestion

Preserve types from function expressions in initializers when emitting declarations.

📃 Motivating Example

// index.ts
type P = {} & { foo: boolean };
export let fn = (p: P, p2:typeof p) => {}

// index-current.d.ts
type P = {} & { foo: boolean };
export declare let fn: (p: P, p2: {
    foo: boolean;
}) => void;
export {};

// index-proposed.d.ts
type P = {} & { foo: boolean };
export declare let fn: (p: P, p2: typeof p2) => void;
export {};

💻 Use Cases
  1. What do you want to use this for?
    This would improvement would can bring us closer to external declaration emitters being a reality.
  2. What shortcomings exist with current approaches?
    An external tool can extract the type from the function expression, but then they would have different declarations. It would be great if, where possible, typescript would also preserve types as written.
  3. What workarounds are you using in the meantime?
    We could force users to always specify the type even when it's obvious from the function expression, but this makes for very frustrating DX

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

No source files, tests, or entry points are named. Reproduce the motivating example and inspect TypeScript's declaration emit behavior; done means function-expression initializer types are preserved as written in the emitted declaration without changing JavaScript output.

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
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.