#Safe accessing of Map?
1 messages · Page 1 of 1 (latest)
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'?
It already errors.
@light tide thanks. I used swc and it seems to pass there
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.
any idea how I can get it typechecked?
oh well how would I achieve that using vscode?
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.
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
so I assume the language server is working, but not conforming to your assumption
at this point I would be happy if it was just "shown in red color" in the file
Do you have a tsconfig.json?
no
Well you need one for language server to know this is a TS project.
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
You need to put stuffs in there, an empty file won't do.
You can take a look at https://www.typescriptlang.org/docs/handbook/tsconfig-json.html and specifically you can use one of the bases in https://github.com/tsconfig/bases