R
R
Roman Chugunov2019-03-11 09:44:52
typescript
Roman Chugunov, 2019-03-11 09:44:52

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"/>

or
/// <reference types="hapi"/>
That TS all the same does not allow to isolzovat interfesa from hapi. It just doesn't see. If I do import, then my .d.ts will already be modules and not header files, and types will have to be constantly imported everywhere that I would like to avoid.
Tell me if there is any solution? To be able to extend interfaces from framework types without importing them, but only somehow referring to them

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Chugunov, 2019-03-13
@Chalovik

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 question

Ask a Question

731 491 924 answers to any question