question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Force Override Declarations Types

See original GitHub issue

Search 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:open
  • Created 4 years ago
  • Reactions:156
  • Comments:30 (4 by maintainers)

github_iconTop GitHub Comments

15reactions
Thyiadcommented, Mar 30, 2021

How it’s going

10reactions
anuouacommented, Nov 10, 2021

Any update?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Typescript Feature Proposal. Force Override Declarations
Force override declaration is basically a method of creating imported override type declaration. Typescript allows you to create a global. d. ...
Read more >
How to force override an existing type in Typescript?
So in an attempt to force override the type, I tried this: // Type 'Schema[keyof Schema & string]' is not assignable to type...
Read more >
Overriding and Hiding Methods (The Java™ Tutorials ...
Instance Methods​​ The overriding method has the same name, number and type of parameters, and return type as the method that it overrides....
Read more >
override modifier - C# Reference | Microsoft Learn
An overriding property declaration must specify exactly the same access modifier, type, and name as the inherited property.
Read more >
12 Rules of Overriding in Java You Should Know
Basically, the overriding method must have same name and same arguments list as the overridden one. It's the way by which a subtype...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found