microsoft / microsoft/TypeScript

In JS, type annotations should not block errors from the rest of the program

Open
#23,472 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Awaiting More Feedback Domain: JavaScript Suggestion
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

In a Javascript file that is checked by flow, we may see code like this (from create-react-app/packages/react-error-overlay/src/utils/parseCompileError.js)

export type ErrorLocation = {|
  fileName: string,
  lineNumber: number,
  colNumber?: number,
|}
function parseCompileError(message: string): ?ErrorLocation {
  const lines: Array<string> = message.split('\n');
  for (let i = 0; i < lines.length; i++) {
// ...................
  return fileName && lineNumber ? { fileName, lineNumber, colNumber } : null;
}

This behaves badly when the typescript compiler compiles it with checkJs on:

Expected behavior:

  1. Errors on the type declaration and all the type annotations.
  2. However, ErrorLocation should be declared as a type alias, and message, lines, etc should all have their declared types.
  3. If they are used incorrectly, they should have errors.

Actual behavior:

  1. Errors on the type declaration and all the type annotations.

In the language service:
2. message, lines, etc have the correct types, but ErrorLocation is type any.
3. If they are used incorrectly, they have errors.

In batch compilation:
2. No errors show up except those from (1), even if there are lots of other javascript files without type annotations.

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 example in create-react-app/packages/react-error-overlay/src/utils/parseCompileError.js and compare language-service behavior with batch compilation. Trace how type declarations and annotations affect ErrorLocation and the inferred types of message and lines. Done means annotation errors remain while unrelated JavaScript files continue to report their own type errors in both modes.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, typescript
Domain
compilers
Issue type
Bug
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.