Home > OS >  Override type declaration when running tsc
Override type declaration when running tsc

Time:05-28

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:

{
  "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"]
}

CodePudding user response:

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) {
  //...
}
  • Related