Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generic type arguments are sometimes also valid JavaScript comparison expressions #33639

Closed
jasvir opened this issue Sep 27, 2019 · 3 comments
Closed
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it

Comments

@jasvir
Copy link

jasvir commented Sep 27, 2019

TypeScript Version: 3.4.0-dev.201xxxxx

Search Terms:
superset, syntactic, semantic, heuristic, parsing

Code

function f(x = {}) {
  document.body.innerText =
    `I'm ${
      arguments[1] === false ?
      "java" :
      "type"
  }script`;
}
let h = () => "";
f(h.apply<String, Object>("type"));

Expected behavior:
When run as JS (since it's valid JS) it should output the same as when run as TS.

Actual behavior:
The behavior is different in JavaScript and TypeScript

Playground Link:
https://www.typescriptlang.org/play/#code/GYVwdgxgLglg9mABMAFAD0QXkQbwL4CUuAsAFCKIAmcEIAtgKZhQB0ARnJQJ4sxhgMATgBUGaKFjIUKAAwCSAcjqIAJDinSKAQ0EBzekygBnANoBGALpZM2YFoA2RhogD8GzQCIAVloBuWj0QALndpDyguAAcGDw08IwhBGEioGQBuMjwyewYJAAssRBQiTAA+RA8PDNJUPJYtSMj7LgAeAGUoJLBdABpEAHk2LwZoUpRwqJiCAmqgA

Related Issues:

#26179

Note

I wrote about this at some length https://medium.com/@jasvir/monoglots-when-a-subset-is-not-1604e3a51d9 because I thought it was just cute but not serious. I was inspired to file it here by a comment @erights made in a bug filed against TC39 (tc39/ecma262#1681) on JS failing to be a superset of JSON that resulted in a security bug.

@RyanCavanaugh RyanCavanaugh added the Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it label Sep 27, 2019
@RyanCavanaugh RyanCavanaugh changed the title TypeScript is a syntactic superset but not a semantic superset of JavaScript Generic type arguments are sometimes also valid JavaScript comparison expressions Sep 27, 2019
@RyanCavanaugh
Copy link
Member

Nice construction!

@guyutongxue
Copy link

We can create a more interesting construction,

let as=()=>0,m=[].map<0>as(()=>[]);console.log(`I'm ${!m?'Java':'Type'}Script`)

This piece of code won't emit any error on a default-configured tsc, and the running result is I'm TypeScript. In a JS runtime, this code produces I'm JavaScript. 🤣

I've also tried some popular TS runtime. The result is:

$ deno
Deno 1.45.5
exit using ctrl+d, ctrl+c, or close()
REPL is running with all permissions allowed.
To specify permissions, run `deno repl` with allow flags.
> let as=()=>0,m=[].map<0>as(()=>[]);console.log(`I'm ${!m?'Java':'Type'}Script`)
I'm TypeScript
undefined
$ bun repl
Welcome to Bun v1.1.24
Type ".help" for more information.
[!] Please note that the REPL implementation is still experimental!
    Don't consider it to be representative of the stability or behavior of Bun overall.
> let as=()=>0,m=[].map<0>as(()=>[]);console.log(`I'm ${!m?'Java':'Type'}Script`)
I'm TypeScript
undefined
$ node --experimental-strip-types
Welcome to Node.js v22.6.0.
Type ".help" for more information.
> (node:6927) ExperimentalWarning: Type Stripping is an experimental feature and might change at any time
(Use `node --trace-warnings ...` to show where the warning was created)
let as=()=>0,m=[].map<0>as(()=>[]);console.log(`I'm ${!m?'Java':'Type'}Script`)
I'm JavaScript
undefined

@jasvir
Copy link
Author

jasvir commented Aug 15, 2024

Nice! There's a shorter construction due to @mikesamuel viz.
console.log(`I'm ${ (0).toFixed.apply<Number, String>(1) ? 'Type' : 'Java' }Script`).

Semantic supersetting is hard - I think it's a dead horse not worth beating further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Won't Fix The severity and priority of this issue do not warrant the time or complexity needed to fix it
Projects
None yet
Development

No branches or pull requests

3 participants