TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Class implementing interface implicit types

Open asotog opened this issue 10 months ago • 1 comments

🔍 Search Terms

Hi all,

is there any plan to allow implicit types when a class is anyways implementing an interface, function types are well defined, but still need to redeclare function types on the class:

Image

thanks in advance

✅ Viability Checklist

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • [x] This isn't a request to add a new utility type: https://github.com/microsoft/TypeScript/wiki/No-New-Utility-Types
  • [x] This feature would agree with the rest of our Design Goals: https://github.com/Microsoft/TypeScript/wiki/TypeScript-Design-Goals

⭐ Suggestion

It would be cleaner if developer is not forced to redeclare function types when implementing functions coming from an interface

📃 Motivating Example

I'd like to propose an enhancement that could significantly reduce code duplication and improve developer experience.

Currently, when a class implements an interface, we're required to redeclare method types even though they're already defined in the interface:

interface UserService { getUser(id: string): Promise<User>; updateUser(user: User): Promise; }

class UserServiceImpl implements UserService { // We have to repeat types even though they're defined in the interface async getUser(id: string): Promise<User> { ... } async updateUser(user: User): Promise { ... } }

Since the class explicitly implements the interface, TypeScript could infer these types without us having to redeclare them:

class UserServiceImpl implements UserService { // Types could be inferred from the interface async getUser(id) { ... } async updateUser(user) { ... } }

This would:

  • Follow the DRY principle
  • Reduce boilerplate
  • Lower maintenance burden when types change
  • Decrease chances of inconsistencies
  • Support TypeScript's goal of type safety with minimal overhead

Is there any plan to implement this feature? It seems like a natural evolution of TypeScript's powerful type inference capabilities.

💻 Use Cases

  1. What do you want to use this for? Write cleaner code
  2. What shortcomings exist with current approaches? It would not be a breaking change
  3. What workarounds are you using in the meantime? No workarounds, still writing again the parameters types for functions

asotog avatar Mar 14 '25 22:03 asotog

Duplicate of https://github.com/microsoft/TypeScript/issues/32082.

whzx5byb avatar Mar 14 '25 23:03 whzx5byb

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

typescript-bot avatar Mar 20 '25 01:03 typescript-bot