microsoft / microsoft/TypeScript

Allow type annotations in .js files in preparation for Type Annotations proposal

Open
#59,648 0 comments 0 reactions 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

🔍 Search Terms

allow js file extension

✅ Viability Checklist
⭐ Suggestion

Add an option to allow removing the error "[some feature] can only be used in TypeScript files"

Previous discussion: #10939

The compiler option would allow consuming .js files as if they were .ts files, but with some differences (see below). All type-checking, type-stripping, and code emitting features would remain as-is.

📃 Motivating Example

The Type Annotations proposal is the definite future of JavaScript.

This feature would:

  • Enable incremental adoption of the Type Annotations proposal by users
  • Enable incremental support of Type Annotations in the TypeScript compiler
  • Possibly help push that proposal forward a little more quickly by increased usage
  • Spread word that this proposal exists by gaining compiler support in tsc and its release notes

It's true that the exact syntax isn't definite yet, and it will probably be at least somewhat different than current TypeScript syntax.

However, that's exactly why adding initial support for plain TypeScript would not interfere with its eventually sealed syntax.

In other words, the initial support would simply parse it as "typed-javascript" but the TypedJavaScript parser would currently be exactly the same as the TypeScript parser for the time being, and allow gradual differentiation.

It would also be a good opportunity to allow TypedJavaScript mode to disallow namespaces, enums, constructor fields, etc.

💻 Use Cases
  1. What do you want to use this for?

Writing future-ready JavaScript.

  1. What shortcomings exist with current approaches?

It doesn't take into account the pending type annotation proposal.

  1. What workarounds are you using in the meantime?

I can almost get this working with some VS Code hacks, but then tsc complains as soon as I run it.

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 by reading the previous discussion in #10939 and the linked TC39 Type Annotations proposal. Then examine the TypeScript compiler's existing restrictions on TypeScript-only syntax in .js files and determine the option's scope, including which constructs remain disallowed. Done means a documented compiler option supports the intended typed-JavaScript parsing without changing emitted runtime behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, 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.