TypeScript is NOT a Superset of Javascript! (Edit: It IS a Superset. I Was Wrong!)

Created on 11 Apr 2020  Β·  6Comments  Β·  Source: microsoft/TypeScript


TypeScript Version: 3.8.3


Search Terms:
Property does not exist on type
Code
Below is valid Javascript however it does not compile with TypeScript and there is no tsconfig.json setting to enable it to work:

let v = {pet: "cat", friend: "Joe"};
v.toy = "hammer";

This is a definition of subset: "a set which includes another set or sets.". The above code comes from Javascript but it can't work in TypeScript without manually adding TypeScript to it via any or // @ts-ignore.

This is a real problem for me right now because I need to port a number of Javascript npm packages to deno TypeScript. As it is now there are hundreds of places in code I need to manually change to make this work because TypeScript isn't actually a superset. If it only had additional configuration variables for tsconfig.json that allowed regular valid javascript it would be a superset.

I need to convert the Javascript to Typescript because the vscode extension that I am using and that is available (https://marketplace.visualstudio.com/items?itemName=axetroy.vscode-deno) tells me where the deno and node.js differences are that I need to handle, but it only works with TypeScript so I need to convert it to TypeScript.

The most frequent error I am getting that I want to suppress globally (but currently can't) is this one: Property 'toy' does not exist on type '{ pet: string; friend: string; }'.

Expected behavior:

I expect there to be more settings available for tsconfig.json to all me to use valid javascript without having to manually add TypeScript.

Unactionable

Most helpful comment

You are looking for a "global setting" to disable TypeScript. That's called not using TypeScript...

All 6 comments

and there is no tsconfig.json setting to enable it to work

"noEmitOnError": false

There are also various ways you can type v to make sense and satisfy TypeScript at the same time. I can't say for sure what's best for you without a more realistic scenario, but some possibilities:

interface Kid { pet: string, friend: string, toy?: string }
type BagOJunk = { [key: string]: string }

@nmain Thank you for pointing out "noEmitOnError": false. I was not aware of how this worked. With this I see that TypeScript really is a superset of Javascript. I was wrong.

It would still be nice if there was a way to globally turn off these errors and only show javascript errors.

I understand the examples you gave me but those are manual solutions. I'm looking for a global settings so I don't have to manually write the kind of code you gave 100 times.

You can add // @ts-ignore in head of file and no type error will show.

You are looking for a "global setting" to disable TypeScript. That's called not using TypeScript...

There's a lot of knobs that you can use to make migration easier, some which have been pointed out:

  • strict set to false (already the default)
  • noEmitOnError set to false (already the default)
  • casts to any
  • // @ts-ignore comments to suppress error messages
  • // @ts-expect-error comments coming up in 3.9 (maybe less relevant in this situation)

And even further in .js files:

  • allowJs to compile .js files without reporting errors
  • checkJs set to false to disable errors when allowJs is on (already the default)
  • // @ts-check comments in .js files to turn on checking for an entire file, so that you can migrate a file at a time
  • // @ts-nocheck comments in .js files to turn off checking for an entire file
Was this page helpful?
0 / 5 - 0 ratings

Related issues

fwanicka picture fwanicka  Β·  3Comments

bgrieder picture bgrieder  Β·  3Comments

uber5001 picture uber5001  Β·  3Comments

blendsdk picture blendsdk  Β·  3Comments

dlaberge picture dlaberge  Β·  3Comments