microsoft / microsoft/TypeScript

checkJs: x.js should use x.d.ts for type inference

Open
#44,946 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Suggestion
Dominant language
Go
Stars
111k
Forks
14.4k
Avg merge
1d 19h
Merged PRs (30d)
117

Description

Suggestion

🔍 Search Terms

checkjs, declaration files

✅ Viability Checklist

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

⭐ Suggestion

If a declaration file with the same name than a JavaScript file is available (in the same location), then the declaration file should be used to check the js file.

📃 Motivating Example

Assume that "checkJs" and "strict" options are enabled.

Given the following files:

// @filename a.d.ts
export function f(n: number): number;
// @filename a.js
function f(a) {
    // Parameter 'a' implicitly has an 'any' type
    return a + 1
}

the type of f should be inferred and should thus type-check. TSC does not currently use a.d.ts to infer types of a.js.
Note that importing f in another js file, already infer f's type

// @filename b.js
import { f } from "./a.js"
// type of f is inferred

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

Start with the checkJs and strict reproducer using a.d.ts, a.js, and b.js, then run TypeScript's checker against it to confirm the current diagnostic. Trace how the declaration file is resolved for the imported f and compare that with checking a.js directly. Done means the same-name declaration is used for a.js type checking without changing emitted JavaScript.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.