Home > Enterprise >  Typings (.d.ts) for JS module
Typings (.d.ts) for JS module

Time:06-08

I'm trying to generate Typescript typings for link2aws so I can use it inside my Angular project. I've generated a .d.ts file, but I still get the error: TypeError: (new link2aws__WEBPACK_IMPORTED_MODULE_1__.ARN(...)).consoleLink is not a function.

I'm importing ARN from link2aws like so:

import { ARN } from 'link2aws';

And calling it like so:

arn = 'arn:aws:...'
let link = new ARN(arn).consoleLink();

link2aws.d.ts

// Type definitions for link2aws
// Project: https://github.com/link2aws/link2aws.github.io#readme
// Definitions by: me <url redacted> 

declare module 'link2aws' {
  class ARN {
    constructor(text: string);
    string(): string;
    console(): string;
    qualifiers(): string[];
    pathLast(): string;
    consoleLink(): string;
  }
}

CodePudding user response:

When you create typings for an existing module, they have to match the sources.

Citing the usage from the docs

new ARN('arn:aws:s3:::abcdefgh1234').consoleLink;

So it seems consoleLink is not a function but a property of ARN.

And when you look at the source code you also see

get consoleLink() { ...} 

which denotes a getter So, indeed, the runtime is right. consoleLink (and probably the other properties too, didn't look too much into the details) is not a function and thus can't be called like one.

So a correct typing for that class may be something like

class ARN {

  ...

  public consoleLink: string;

}
  • Related