microsoft / TypeScript

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

In JSDocs notation for index signatures, Object and object should be treated as the same #20555

Open michaeljota opened 6 years ago

michaeljota commented 6 years ago

TypeScript Version: 2.7.0-dev.201xxxxx

Code

/**
 * Test.
 *
 * @param {object.<string, number>} obj An object.
 */
function func(obj) {
    console.log(obj);
}

Expected behavior:

obj should have a index signature as in #15105

Actual behavior:

obj is treated as any

Additional Info:

This was mentioned in the above issue, and it seems to be related to the way Typescript manage Object and object, but in JSDocs, they are treated similar.

brettz9 commented 6 years ago

My concern with treating them "the same" in this context (if that means losing the TypeScript distinction between object and Object) is that with objects created by Object.create(null) not passing instanceof Object, object comes across more as the generic "typeof(val) === 'object'" (not excepting null, arrays or functions), whereas Object suggests to me an object that is an instance of Object.

brettz9 commented 2 years ago

My concern with this is that with objects created by Object.create(null) not passing instanceof Object, object comes across more as the generic "typeof(val) === 'object'" (not excepting null, arrays or functions), whereas Object suggests to me an object that is an instance of Object.

Besides breaking this helpful distinction of benefit to JavaScript users familiar with the type differences, treating object and Object as fully the same could be more confusing to TypeScript users and those transitioning to it.

While I normally support greater JSDoc compatibility, in my personal opinion, you should avoid introducing more distinctions between the TypeScript JSDoc flavor and the TypeScript proper syntaxes. It's bad enough for compatibility that one can't go reliably in the other direction, e.g., so we one use JSDoc types like Object<string, string> as a proper TypeScript type (yes, it is more verbose, but good for compatibility).

Of course if you just mean "Don't treat object.<string, number> as "any", then that makes sense.