Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Override type declaration when running tsc

I have a library I’m attempting to convert over to TypeScript. For now I’m allowing JavaScript, and simply compiling the project with the declaration flag turned on so it can be consumed by other TypeScript projects without errors. Everything else seems to export correctly, except for one method which is being typed as any for the comp arg.

export function setup(comp: any[]): void;

Is there a way I can override the types for this specific method, either it be through some sort of syntax so I can declare the function arg that setup expects without moving the file to a *.ts (yet)

My tsconfig.json file looks like the following:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": false,
    "declaration": true,
    "declarationDir": "./dist",
    "declarationMap": true,
    "emitDeclarationOnly": true,
    "experimentalDecorators": true,
    "lib": ["es2019", "dom"],
    "module": "es2015",
    "moduleResolution": "node",
    "skipLibCheck": true,
    "target": "es2019",
    "typeRoots": ["node_modules/@types"],
    "useDefineForClassFields": false
  },

  "include": ["src/**/*.ts", "src/**/*.js"]
}

>Solution :

I believe you can use JSDoc type comments to set the type from a *.js which Typescript will pickup and use.

For example:

// myfile.js

/**
 * @type {(comp: number[]) => void}
 */
export function setup(comp) {
  //...
}

Or maybe:

// myfile.js

/**
 * @param {number[]} comp - the array of comp stuff
 */
export function setup(comp) {
  //...
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading