declaration
module
merging
override
interface
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.
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.
// 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.
My suggestion meets these guidelines:
I guess this would work so long as there's only one file like this. I do have some apprehension since users might use this more often in place of actually fixing things on DefinitelyTyped.
This could be used as a temporary measure because type definitions have to be reviewed before being updated on DefinitelyTyped. There also no need for one override file, since you can create a user based folder called override and place declaration files with the name of the module your trying to override. Because files can鈥檛 have the same name there will be no duplicates of modular overrides.
I want this feature too. I use webpack in my project, it has declare the process variable, just like this:
declare global {
interface __WebpackProcess {
env: any
}
declare var process: __WebpackProcess
}
It declare process.env as any type, but I want change it to a more accurate type, I don't know how to do it.
This is like the dual of #31894.
I have a need for this too. For example, I'd like to tighten up the type for QUnit's assert.ok to
ok(state: boolean, message?: string): void;
in a project (but don't think the DT types should be that strict). There's no good way to do this (at least that I'm aware of) short of redefining all of the QUnit types locally and not including @types/qunit.
I have a similar need, where I imported a library which is partly typed, and when I do declaration merging/module augmenting for that library, I'm being locked in "declarations must have the same type" issue for days.
It would be useful also for "special cases" where typings are used to discourage use of API params which are actually valid. E.g. Stipe's create source API method: https://github.com/stripe/stripe-node/issues/974
This would also be nice to have if you are writing a plugin for a library which has the correct types without the plugin, currently the only way to override the lib tyes is to either fork a fixed version of the original lib鈥檚 typings or use patch-package like I鈥檓 doing now https://www.npmjs.com/package/@foodsy-app/fastify-typebox#important-note-before-using-this-plugin
I'm running into similar problems: I have a library that uses JSX but not React. Since React types are global, when both my library and React are installed, the types clash. I would love to be able to import my types just in the files that use my library without disturbing the typing in other files.
+1! Just wanted to add another use case for this I've encountered:
We use the TypeORM library in a large web application and a few of it's "findBy"-style methods are overly permissive. It's easy to pass arguments which actually have no effect on the generated SQL but seem like they would. We'd like to disallow certain usages as a team by narrowing the type declarations but the package provides it's own types so our only option is to fork it. We'd love to have an overrides file where we could forcibly declare a narrower input type for just a few methods and call it a day!
@bengotow you could prevent that by writing a custom tslint rule
Most helpful comment
I want this feature too. I use webpack in my project, it has declare the
processvariable, just like this:It declare
process.envasanytype, but I want change it to a more accurate type, I don't know how to do it.