Answer the question
In order to leave comments, you need to log in
How to extend from interface from another .d.ts?
Hello everyone)
Not so long ago, at work, they began to translate a large project into TS. We have a monorep and everything is in JS, front, back and mobile.
And accordingly, there is a need to move all types to the root and dependencies to the root node_modules, and define global types and const enums so that types and interfaces can be used globally without import { ... } from '...'; But in order to achieve this, .d.ts files should not contain imports and exports, otherwise these files will be considered modules and you will have to write import { ... } from '...' everywhere; All types and interfaces are defined through declare.
No matter how good everything is, I took out many types and enums globally, divided them into namespaces, everything is beautiful. Everything is working. BUT when it was necessary to extend the interface from the framework, then there were already problems.
For example with Hapi.Request. There is hapi at the root in node_modules/@types.
When in .d.ts I write:
/// <reference path="../node_modules/@types/hapi/hapi.d.ts"/>
/// <reference types="hapi"/>
Answer the question
In order to leave comments, you need to log in
The problem is solved very, very simply.
You can declare a type inside the namespace that refers to any other type from the import:
declare namespace Requests {
type Request = import("hapi").Request;
interface Example extends Request {...}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question