visual studio 2015 - TypeScript .d.ts sözdizimi - export ve declare


.d.ts dosyası oluşturmanın doğru yolunu anlamaya çalışırken yardıma ihtiyacım var.

Beni şaşırtan şey, bazı insanların bu sözdizimini kullanması:

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

karşı

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

karşı

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

veya

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

karşı

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

Hangisi doğru? Declare ne için kullanılır? Export ne için kullanılır? Namespace ve module ne zaman kullanılır?

Cevap #1

Doğru yol şudur:

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

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

Doğru imzayı her zaman bazı .ts dosyaları yazarak ve bunları --declaration seçeneği ile derleyerek kontrol edebilirsiniz (tsc test.ts --declaration). Bu, doğru türleri içeren bir d.ts dosyası oluşturacaktır.

Örneğin, yukarıdaki beyan dosyası aşağıdaki koddan oluşturulmuştur:

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;
        }
    }
}

İlgili makaleler