3

I am trying to map a library (not written by me) to a .d.ts file. It has an object that is a function but that has properties (some of them also functions), like this:

var asd = function () { return 1; };
asd.two = function () { return 2; };
asd.three = 'three';

How can I write the type of asd in a .d.ts file? How to specify that it is a function that returns a number and that has two properties, one a function returning a number and one that is a string

pistacchio
  • 56,889
  • 107
  • 278
  • 420
  • Possible duplicate of [build a function object with properties in typescript](http://stackoverflow.com/questions/12766528/build-a-function-object-with-properties-in-typescript) – mk. Jan 29 '16 at 14:54
  • @mk. I think that question is slightly different because it's not about definition files, but I do believe I saw this question somewhere else before... I just can't find it. – David Sherret Jan 29 '16 at 15:00
  • @DavidSherret Yep, you're right, it's not exact, though the answers are just about the same except for `declare`. I updated my answer in that other question to cover .d.ts files. – mk. Jan 29 '16 at 15:21

1 Answers1

5

Use declaration merging:

declare function asd(): number;

declare module asd {
    function two(): number;
    var three: string;
}

Tests:

let num: number = asd();
num = asd.two();
asd.three = "str";
Kinrany
  • 99
  • 1
  • 9
David Sherret
  • 101,669
  • 28
  • 188
  • 178