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

3
  • Possible duplicate of build a function object with properties in typescript Commented Jan 29, 2016 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. Commented Jan 29, 2016 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. Commented Jan 29, 2016 at 15:21

1 Answer 1

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";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.