Skip to content

inherit types from base classΒ #52002

Closed
Closed
@Zain-ul-din

Description

@Zain-ul-din

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions