Skip to content

JS value being parsed as a TS type argument #36662

Closed
@shicks

Description

@shicks

TypeScript Version: 3.8.0-dev.20200205

Search Terms: emscripten "type arguments"

Code

The following code (I added the declaration) was found in the distribution package for viz.js: https://github.com/mdaines/viz.js/releases/download/v2.1.2/lite.render.js. This file is generated by emscripten, so the syntax is a little weird.

declare let [k, e, a, c, n]: number[];
if ((k | 0) < (e | 0) | (k | 0) > (a << 16 >> 16 | 0)) {
              c[n >> 2] = 1364;
}

Expected behavior:

This is valid JavaScript and should have the same parse as JS.

Actual behavior:

If this is parsed as TS, the part between the < and the > get pulled out as a type annotation, leaving

if ((k | 0)(a << 16 >> 16 | 0))

which is clearly incorrect, as it's trying to call a number. If this is parsed as a JS file (with --allowJs), it produces an error:

$ tsc --allowJs a.js
a.js:2:14 - error TS8011: 'type arguments' can only be used in a .ts file.

2 if ((k | 0) < (e | 0) | (k | 0) > (a << 16 >> 16 | 0)) {
               ~~~~~~~~~~~~~~~~~~

Even if one makes the argument that this is not valid TS syntax, it should still parse the explicitly-JS input.

Playground Link:

https://www.typescriptlang.org/play/?ts=3.8.0-dev.20200205&ssl=1&ssc=1&pln=4&pc=2#code/CYUwxgNghgTiAEEQBd4G0DWAaeIdRzBwDsBdALnmIFcBbAIxBjVIG4AoASwDN4AKPhngAfeAAYAlPAA8-BKMkj+QhVIB8-KDNkBGAGzw1G-UslSA3u3jWbt22DTFDGgEyl4AXng6AzHoAsHAC+QA

Related Issues:

I was unable to find anything.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Breaking ChangeWould introduce errors in existing codeBugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions