Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeScript .d.ts syntax - export and declare

I need help trying to understand what is the correct way to create a .d.ts file.

What threw me is that some people use this syntax:

// lib-a.d.ts
namespace My.Foo.Bar {
  interface IFoo {}
  interface IBar {}
}

vs.

// lib-b.d.ts
declare namespace My.Foo.Bar {
  interface IFoo {}
  interface IBar {}
}

vs.

// lib-c.d.ts
namespace My.Foo.Bar {
  export interface IFoo {}
  export interface IBar {}
}

vs.

// lib-d.d.ts
declare namespace My.Foo.Bar {
  export interface IFoo {}
  export interface IBar {}
}

vs.

// lib-e.d.ts
declare module My.Foo.Bar {
  export interface IFoo {}
  export interface IBar {}
}

Which one is correct? What is declare used for? What is export used for? When to use namespace vs. module?

like image 712
Mohamed Nuur Avatar asked Jan 17 '17 01:01

Mohamed Nuur


People also ask

What is the difference between .ts and D ts?

ts allows a subset of TypeScript's features. A *. d. ts file is only allowed to contain TypeScript code that doesn't generate any JavaScript code in the output.

What is declaration D ts?

Declaration files, if you're not familiar, are just files that describe the shape of an existing JavaScript codebase to TypeScript. By using declaration files (also called . d. ts files), you can avoid misusing libraries and get things like completions in your editor.

What is D ts in TypeScript?

The "d. ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using something like jQuery or underscore, an existing javascript library. You want to consume those from your typescript code.

What is index D ts in TypeScript?

*. d. ts files are used to provide typescript type information about a module that's written in JavaScript, for example, underscore / lodash / aws-sdk. This will allow you to use the javascript modules without the need to convert them to ts without getting any type of error on your code.


1 Answers

The correct way is:

declare namespace NS {
    interface InterfaceTest {
        myProp: string;
    }

    class Test implements InterfaceTest {
        myProp: string;
        myFunction(): string;
    }
}

You always can check the correct signature by writing some .ts file and compiling it with the --declaration option (tsc test.ts --declaration). This will generate a d.ts file with the correct typings.

For example the above declaration file was generated from the following code:

namespace NS {
    export interface InterfaceTest {
        myProp: string;
    }

    export class Test implements InterfaceTest {
        public myProp: string = 'yay';

        public myFunction() {
            return this.myProp;
        }
    }   

    class PrivateTest implements InterfaceTest {
        public myPrivateProp: string = 'yay';

        public myPrivateFunction() {
            return this.myProp;
        }
    }
}
like image 192
NoNameProvided Avatar answered Sep 23 '22 01:09

NoNameProvided