google / closure-compiler

A JavaScript checker and optimizer.
https://developers.google.com/closure/compiler/
Apache License 2.0
7.34k stars 1.14k forks source link

Plain Object and IObject are not compatible anymore? #2970

Open vobruba-martin opened 6 years ago

vobruba-martin commented 6 years ago

I didn't use the latest version of Closure Compiler for some time but I've upgraded to the latest version a I'm surprised by these warnings in my projects:

/** @type {!IObject<string,string>} */
var a = {};

/** @type {!IObject<string,string>} */
var b = {
    'foo' : 'bar'
};
test.js:2: WARNING - initializing variable
found   : {}
required: IObject<string,string>
var a = {};
        ^^

test.js:5: WARNING - initializing variable
found   : {foo: string}
required: IObject<string,string>
var b = {
        ^

0 error(s), 2 warning(s), 100.0% typed

But if I force the type, warnings disapear.

/** @type {!IObject<string,string>} */
var a = /** @type {!IObject<string,string>} */ ({});

/** @type {!IObject<string,string>} */
var b = /** @type {!IObject<string,string>} */ ({
    'foo' : 'bar'
});
0 error(s), 0 warning(s), 100.0% typed

Command line params:

    --compilation_level ADVANCED \
    --language_in ECMASCRIPT6_STRICT \
    --language_out ECMASCRIPT5_STRICT \
    --new_type_inf \
    --source_map_format V3 \
    --summary_detail_level 3 \
    --use_types_for_optimization \
    --warning_level VERBOSE
vobruba-martin commented 6 years ago

I've found the commit after which warnings appeared: cb0069aac5e743a9d450dd2c0de411c5055043b8

Why can't be NTI used anymore?

brad4d commented 6 years ago

I've just updated our wiki page for NTI with an explanation for why it is gone.

https://github.com/google/closure-compiler/wiki/%5BOBSOLETE%5D-Using-NTI-(new-type-inference)

vobruba-martin commented 6 years ago

@brad4d Thank you for that! What about this issue? Will it be fixed?

concavelenz commented 6 years ago

This was implemented incorrectly in NTI. IObject is used to declare the "computed property operator". It can't be inferred. "Object<string,string>" is what you want here.