microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.08k stars 12.37k forks source link

Quick fix for using bracket accessor instead of get/set on Map #32925

Open mjbvz opened 5 years ago

mjbvz commented 5 years ago

TypeScript Version: 3.6.0-dev.20190814

Search Terms:

Code With strict: true:

class Foo {
    public readonly map = new Map<string, string>();

    constructor() {
        console.log(this.map['af123']);``
    }
}

This produces the error:

Element implicitly has an 'any' type because type 'Map<string, string>' has no index signature. Did you mean to call 'get' ?

Expected behavior: The error message suggests a fix. We should should be a quick fix that changes the code to call .get

Actual behavior: No quick fix returned.

RyanCavanaugh commented 5 years ago

Nice idea πŸ™‚

bhushan-shah-dev commented 5 years ago

Wanted to try this out as a first issue.

I think I need to create a new fix within src/services/codefixes that will apply to Diagnostics.Element_implicitly_has_an_any_type_because_type_0_has_no_index_signature_Did_you_mean_to_call_1 error code.

Any further pointers?

r3a1d3a1 commented 1 month ago

Do you guys know how to simply use square brackets w/o getting that error?! I don't like verbosity of 'get'.

RyanCavanaugh commented 1 month ago

Do you guys know how to simply use square brackets w/o getting that error?!

The problem with square brackets is that they don't work...

r3a1d3a1 commented 1 month ago

It works when using Record, but I'm not sure if it's exactly equivalent.

RyanCavanaugh commented 1 month ago

Record and Map are different things. See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map