#Safe accessing of Map?

1 messages · Page 1 of 1 (latest)

potent veldt
#

let map = new Map<number, number>();
map[3] = 5; //is there any way to get an error for this? maybe tooling
.set method should be used. the code is valid, but it will cause bugs difficult to find

light tide
#
let map = new Map<number, number>();
map[3] = 5;
#

!ts

lapis wagonBOT
#
let map = new Map<number, number>();
map[3] = 5;
//^^^^
// Element implicitly has an 'any' type because type 'Map<number, number>' has no index signature. Did you mean to call 'map.set'?
light tide
#

It already errors.

potent veldt
#

@light tide thanks. I used swc and it seems to pass there

light tide
#

Tools like swc do not type check, they only transpile.

#

You still need a step with tsc to do the type checking (which is slow), while you use tools like swc to do transpiling (which is fast) for hot reload/building.

potent veldt
#

any idea how I can get it typechecked?

#

oh well how would I achieve that using vscode?

light tide
#

If you mean for a build step, you can just call tsc -p . --noEmit, usually I have it as a script in package.json

#

In VS Code you should already get the error provided by TS language server.

potent veldt
#

ok I pasted the code you evaluated in a new file x.ts
VSCode shows the syntax is recognized as typescript in the bottom right corner

#

it recognizes other errors, but the error you showed is not detected

potent veldt
#

at this point I would be happy if it was just "shown in red color" in the file

light tide
#

Do you have a tsconfig.json?

potent veldt
#

no

light tide
#

Well you need one for language server to know this is a TS project.

potent veldt
#

I'll try that

#

I'm confused about the matter:
does tsconfig.json belong to a certain vscode extension?

#

just adding an empty file with that name has no effect

light tide
#

You need to put stuffs in there, an empty file won't do.

potent veldt
#

ok thanks

#

i just figured out:

#

tsc --init
creates a proper file so exactly what you said happens

#

meaning "it puts stuff in there"
so that the discussed error finally shows

#

!resolved

#

thank you a lot!

#

It just required strict mode

{
  "compilerOptions": {
    "strict": true
  }
}
light tide
#

There are tons of options in tsconfig.json, if this is a learning project then do take some time and look at some of the options used by the bases and try them out.

#

But yeah strict is the bare minimum to make TS useful.