2
votes

I have an NPM package that has an object as follows:

const Lipsum = function constructor(argOne, argTwo) {
...
}

Object.assign(Lipsum.prototype, {
    firstMethod: function firstMethod(firstArg, secondArg){...}
    secondMethod: function secondMethod(){}
})

module.exports = Lipsum;

I want to declare an ambient TypeScript class for it. I've created a file in my project shared/lipsum.d.ts and wrote the following:

declare class Lipsum {
    constructor(argOne: string, argTwo: number)

    firstMethod(firstArg: string, secondArg: string): string
    secondMethod(): void
}

export default Lipsum;

I'm facing the following issue:

const x = new Lipsum(); // <--- this doesn't throw an error, even though the constructor must have two arguments.
x. // <--- It doesn't autocomplete here, but it autocompletes above when I put a dot after "Lipsum()".

Update:

I attempted to wrap the class with a module declaration, like so:

declare module 'lipsum'{
    export default class Lipsum {
        constructor(argOne: string, argTwo: number)

        firstMethod(firstArg: string, secondArg: string): string
        secondMethod(): void
    }
}

It keeps telling me:

Invalid module name in augmentation. Module 'lipsum' resolves to an untyped module at '/Users/yaharga/project/node_modules/lipsum/lipsum.js', which cannot be augmented.

I should note the code is in a file 'lipsum.d.ts' already.

Turns out all the imports need to be inside the "declare module" scope. Thanks to @drag13 for pointing me in the right direction.

For more information regarding the error I was getting, I used this post for reference.

1

1 Answers

1
votes

You need to specify the module your typing related to. Just add module definition like here

declare module 'MODULE_NAME' {
   class Lipsum {
        constructor(argOne: string, argTwo: number)

        firstMethod(firstArg: string, secondArg: string): string
        secondMethod(): void
    }

    export default Lipsum;
}