Force Override Declarations Types
See original GitHub issueSearch Terms
declaration module merging override interface
Suggestion
Add a way to force override declarations for modules in the workspace. Typescript allows you to create a global.d.ts to add global declarations. How about an override.d.ts or an *.override.d.ts file to override existing modules declarations in the user workspace.
Use Cases
Lets say you install a node module package and type. You see that the type parameter isnโt type safe so you use declaration merging to create a more type safe declaration. However, when you use the module type as a variables type or choose to extending it, it automatically uses the types folder declaration first since the type parameter used fits the node_modules declaration type parameter, however that type would not fit the type parameter the user created. This is because declaration merging selects the most appropriate types in order. If the type was to not match the first declaration type it would move on until reaching the appropriate type, which wouldnโt work in this use case.
Examples
// Node Module module declaration file
declare module "react-router" {
interface RouteComponentProps<Params extends { [K in keyof Params]?: string | undefined }> {
params: Params;
}
}
// User Defined Type declaration file
export type NoRequired<T extends {}> = {
[C in keyof T]: T[C] extends Required<T>[C] ? never : T[C];
};
declare module "react-router" {
interface RouteComponentProps<Params extends NoRequired<Params> }> {
params: Params;
}
}
// Using the Interface
import { RouteComponentProps } from "react-router";
// I don't want to extend RouteComponentProps<{ page: string }>, but I can.
export interface CRUDComponentProps
extends RouteComponentProps<{ page: string }> {
serverName: string;
clientName: string;
}
As you can see the type is accepted as the node modules declaration type instead of a user defined declaration type. There no other way of overriding it other than removing it manually from the the node_modules type file.
Checklist
My suggestion meets these guidelines:
- A way of force overriding a type (preferably an override.d.ts or an *.override.d.ts file)
- A way to sort the order in which declaration merging happens
Issue Analytics
- State:
- Created 4 years ago
- Reactions:156
- Comments:30 (4 by maintainers)
Top GitHub Comments
How itโs going
Any update?