Home:ALL Converter>Is it typical to define interfaces for Typescript files only in declaration files?

Is it typical to define interfaces for Typescript files only in declaration files?

Ask Time:2020-02-06T15:58:54         Author:Ryan Leach

Json Formatter

At work we have a large project that was originally created with namespaces on typescript 1.x, after being migrated from a mix of global scripts, and Immediately Executed functions assigned to variables to mimic classes, in JavaScript.

It's currently on typescript 2.5, and most (but not all) of the interfaces (we write for our classes) are defined in .d.ts files.

In addition, there are quite a few out-of-date .d.ts files for external libraries, that cause errors that have been worked around because we are not using @Typed packages, and the versions of the types don't necessarily match the js library OR the current version of typescript, this is clearly a bad situation.

If I delete all the .d.ts files, I get errors, because understandably it's the only place our interfaces are defined.

I asked someone why it was done this way, and I was told that declaration files in TS for interfaces can act as C headers do, speeding up compilation, type checking, and assist external libraries accessing the code.

Therefore I think the original intention, was to have these files as a form of early module, but now our project has gotten messy using both es6 style import/export statements,

namespace place.UI.group {
    import IDetailsConfiguration = Configuration.IDetailsConfiguration;
    import IGroupConfiguration = Configuration.IGroupConfiguration;
    import LookupValueDataService = Data.LookupValueDataService;

    export class GroupViewModel extends kendo.data.ObservableObject {

and commented out references to old .d.ts files e.g.

/// <reference path="../../../../typings/jquery.d.ts" />

/// <reference path="../../../typings/Config/IBillsConfiguration.d.ts"/>

and I fear that a subtle bug may arise somewhere due to developers not understanding the differences between namespaces and es6 modules (Myself amongst them!).

Should TypeScript Interfaces Be Defined in *.d.ts Files answers the question of interfaces in declaration files nicely, But does separating out the interfaces speed up inference / compilation / analysis? And if so, is there a better method of separating the public interfaces out of theses pseudo-module namesspaces, then manually creating .d.ts files, when the types are already available? Or is the tsc compiler smart enough to do this with the right compiler flags?

Author:Ryan Leach,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60090136/is-it-typical-to-define-interfaces-for-typescript-files-only-in-declaration-file
yy