Skip to content

Request for a new interface (or whatever) to declare object types in TypeScript #53408

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

Closed
jabbar-hafizh opened this issue Mar 21, 2023 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript

Comments

@jabbar-hafizh
Copy link

Feature Request

Describe the feature you'd like

I would like to be able to define a variable with the type of an object in TypeScript. Currently, the syntax let theObject: object is deprecated and there is no other way to declare a variable as an object type.

Describe how you'd like the feature to work

I propose adding a new interface called IObject, which would allow us to define variables with the type of an object in a way that is both clear and concise. Here is an example of what the interface might look like:

interface IObject {
  [key: string]: unknown;
}

With this interface, we could then define an object variable like this:

let myObject: IObject = {
  key1: "value1",
  key2: 42,
  key3: { nestedKey: true }
};

Provide examples of how the feature would be used

Here are some examples of how this new feature would be used in TypeScript:

let myObject: IObject = {
  key1: "value1",
  key2: 42,
  key3: { nestedKey: true }
};

let myOtherObject: IObject = {
  key1: "another value",
  key2: 99,
  key3: { nestedKey: false }
};

Describe any potential drawbacks or challenges

One potential challenge of this feature is that it may not be as performant as other object types, since the [key: string]: unknown syntax allows for any key-value pair. However, I believe that the flexibility and clarity of the IObject interface would outweigh any performance concerns.

Additional Context

What motivated you to propose this feature?

I find that declaring variables as object types already deprecated. I believe that the IObject interface would provide a clear and concise way to define variables as objects in TypeScript.

Are there any existing solutions or workarounds?

Currently, the object type is deprecated and there is no other built-in way to define a variable as an object type in TypeScript. However, we can use an index signature to define an object type, like so:

let myObject: { [key: string]: unknown } = {
  key1: "value1",
  key2: 42,
  key3: { nestedKey: true }
};

While this works, I believe that the IObject interface would provide a more elegant and readable solution.

Would you be willing to contribute this feature?

While I am not currently in a position to contribute to the development of this feature, I would be happy to provide feedback and testing during the development process.

@MartinJohns
Copy link
Contributor

MartinJohns commented Mar 21, 2023

You forgot to fill out the issue template for feature requests.

See #39522 (comment):

We've opted to not include utility type aliases in the lib unless they're required for declaration emit purposes.

Besides that, the I prefix is highly unusual in TypeScript.

And lastly: There is already such a type: Record<string, unknown>.

@fatcerberus
Copy link

Also, since when is object deprecated?

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Declined The issue was declined as something which matches the TypeScript vision labels Mar 21, 2023
@RyanCavanaugh
Copy link
Member

object isn't deprecated. I don't see what problem this is solving in light of that information.

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Declined" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Declined The issue was declined as something which matches the TypeScript vision Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants