Skip to content
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

inherit types from base class #52002

Closed
Zain-ul-din opened this issue Dec 23, 2022 · 5 comments
Closed

inherit types from base class #52002

Zain-ul-din opened this issue Dec 23, 2022 · 5 comments
Labels
Duplicate An existing issue was already created

Comments

@Zain-ul-din
Copy link

Suggestion

🔍 Search Terms

inheriting base types into child class without importing them manually

⭐ Suggestion

In TypeScript v4.9.4, if we extend a class from another file, we also need to import the corresponding types in order to access the base types. I propose that TypeScript should include a feature called "type inheritance" that allows us to access the base types without having to import them.

📃 Motivating Example

Here is some examples using V4.9.4

   // pkg.ts
   export default abstract class Base {
       public get (req: Request, res: Response): Promise<void> {}
   }

  
   /// client side
  
   import Base from '@pkg'
   import { Request, Response } from '@pkg'
   
   class Child extends Base {
       public get (req: Request, res: Response ): Promise<void> {}  
   }
   

Here are some issues with this code:-

  • It is unnecessary to import the types Request and Response in the child class if the base class has already been imported. This adds unnecessary code and complexity.

  • There is no code intelligence in the child class to show which methods are defined in the base class, making it harder to understand and work with the code.

I think this is not a good idea to import types if we have already imported the class Base and also there is no code intelligence in the child class for which method to override or which already defines inside the base class.

💻 Use Cases

The proposed feature, "type inheritance," would allow for the following syntax in TypeScript:

   // pkg.ts
   export default abstract class Base {
       public get (req: Request, res: Response): Promise<void> {}
   }

  
   /// client side
  
   import Base from '@pkg'
   
   class Child extends Base {
       public get (req: inherit, res: inherit): inherit {}  
   }
   

Expected behavior:-

  • With this syntax, the compiler would recognize the types from the base class and provide code intelligence to show which methods
    are defined in the base class. It would also throw an error if the method is not available in the base class.
@fatcerberus
Copy link

fatcerberus commented Dec 23, 2022

Duplicate of the more comprehensive proposal #36165

@Zain-ul-din
Copy link
Author

@fatcerberus do you have any plan to add this feature in upcoming versions?

@fatcerberus
Copy link

I’m not a TypeScript developer but maybe if you’re good Santa will bring you a new feature or two… probably not this one though

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jan 4, 2023
@typescript-bot
Copy link
Collaborator

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

@Zain-ul-din
Copy link
Author

@RyanCavanaugh when we'll see this fix in typescript?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants