Joshua Frank
Joshua Frank

Reputation: 13848

How to define properties on a type defined by a javascript function?

I need to work with a legacy library that's plain old javascript, but which I have renamed with a .ts extension so I can gradually refactor it. This is mostly working, but the code does one thing that Typescript doesn't like:

function TestUtil() {}

TestUtil._startTime;

and the Typescript compiler is complaining that Error TS2339 (TS) Property '_startTime' does not exist on type 'typeof TestUtil'.

I cannot find any syntax that will let me indicate that TestUtil should be treated like any, so that I don't get this kind of compilation error. How can I get around this

Upvotes: 0

Views: 36

Answers (2)

Evert
Evert

Reputation: 99841

If you want TestUtil to be defined as any (which is probably not recommended), you can do this as such:

const TestUtil:any = function() {}

Upvotes: 1

Mário Garcia
Mário Garcia

Reputation: 575

Shouldn't the _startTime var be declared inside the function? The function TestUtil has no var _startTime declared inside of it

Maybe convert it to a class and set _startTime as an static attribute?

class TestUtil() {
    static _startTime: any;
}

Upvotes: 1

Related Questions