Skip to content

In JS, object is treated as 'any' #25624

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
ghost opened this issue Jul 13, 2018 · 9 comments
Open

In JS, object is treated as 'any' #25624

ghost opened this issue Jul 13, 2018 · 9 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature checkJs Relates to checking JavaScript using TypeScript Domain: JavaScript The issue relates to JavaScript specifically Domain: JSDoc Relates to JSDoc parsing and type generation Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Suggestion An idea for TypeScript

Comments

@ghost
Copy link

ghost commented Jul 13, 2018

TypeScript Version: 3.0.0-dev.20180711

Code

/** @typedef {object} T */
/** @type {T} */
const x = 0;

Expected behavior:

Error at x: 0 is not an object.

Actual behavior:

No error.

@ghost ghost assigned sandersn Jul 13, 2018
@ghost ghost added Bug A bug in TypeScript Domain: JSDoc Relates to JSDoc parsing and type generation Salsa checkJs Relates to checking JavaScript using TypeScript and removed Salsa labels Jul 13, 2018
@mhegazy mhegazy added this to the TypeScript 3.1 milestone Jul 13, 2018
@sandersn
Copy link
Member

I think this is a result of Object/object being treated as any. For example, /** @typedef {{}} T */ produces an error.

@sandersn
Copy link
Member

In getTypeFromTypeNode, we have:

case SyntaxKind.ObjectKeyword:
    return node.flags & NodeFlags.JavaScriptFile ? anyType : nonPrimitiveType

This was originally added in #17082. We could revisit this decision, but case-insensitive primitives and Object as any are good decisions in my opinion.

@sandersn sandersn changed the title @typedef with no properties treated as 'any' In JS, object is treated as 'any' Jul 16, 2018
@sandersn sandersn added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 16, 2018
@sandersn sandersn closed this as completed Aug 9, 2018
@ghost ghost added the Suggestion An idea for TypeScript label Oct 9, 2018
@ghost ghost reopened this Oct 9, 2018
@ghost
Copy link
Author

ghost commented Oct 9, 2018

Solution may be to add a new type which is definitely the same as ts's object regardless of what kind of file it appears in.

@ghost ghost removed Bug A bug in TypeScript Working as Intended The behavior described is the intended behavior; this is not a bug labels Oct 9, 2018
@sandersn
Copy link
Member

Maybe type NonPrimitive = object in lib.d.ts?

@weswigham weswigham added In Discussion Not yet reached consensus Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript labels Nov 6, 2018
@weswigham weswigham removed the Domain: JavaScript The issue relates to JavaScript specifically label Nov 29, 2018
@weswigham weswigham added Domain: JavaScript The issue relates to JavaScript specifically and removed Domain: JavaScript The issue relates to JavaScript specifically Salsa labels Nov 29, 2018
@sandersn sandersn modified the milestones: TypeScript 3.4.0, Backlog Mar 13, 2019
@RyanCavanaugh RyanCavanaugh added Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature and removed In Discussion Not yet reached consensus labels Jul 26, 2019
@RyanCavanaugh RyanCavanaugh removed this from the Backlog milestone Jul 26, 2019
@thorn0
Copy link

thorn0 commented Feb 21, 2020

can be closed as fixed by #35661 ?

@sandersn
Copy link
Member

Maybe. #35661 only applies when noImplicitAny: true though.

@ExE-Boss
Copy link
Contributor

I’d like if there was a way to use the object type in JSDoc without needing to enable noImplicitAny.

@sandersn
Copy link
Member

@ExE-Boss you can use the type NonPrimitive = object in one of your own d.ts files. Unfortunately, lots of existing JS code uses object to mean any or unknown, even projects like webpack that compile regularly with typescript.

@ExE-Boss
Copy link
Contributor

@sandersn I’ve personally been using:

/** @typedef {(typeof Object.create) extends (arg0: infer T, ...args: any) => any
	? (T extends null | undefined ? never : T) : never} obj */

Which is the inlined form of:

/** @typedef {NonNullable<Parameters<typeof Object.create>[0]>} obj */

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature checkJs Relates to checking JavaScript using TypeScript Domain: JavaScript The issue relates to JavaScript specifically Domain: JSDoc Relates to JSDoc parsing and type generation Domain: lib.d.ts The issue relates to the different libraries shipped with TypeScript Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

7 participants