How can you include inherited properties in type definitions generated from .js files by tsc?

Viewed 37

I am trying to add typescript support to a vanilla javascript library via .d.ts files. I am trying to generate these type definition files with the "tsc" command, but I am running into many "property 'propertyName' does not exist on type 'typeName'" errors.

This is happening on lines that attempt to call a function on an instance of a child class when that function is defined only in the code for its parent class. It seems that while JSDoc is capable of inferring inherited functions by using "@extends" in the JSDoc comments of a child class, tsc is not.

For example:

ParentClass.js

/**
* This is the parent class.
* 
* @class
*/
export class ParentClass {
  parentFunction(){
    console.log("Running parent function");
  }
}

childClass.js

/**
* This is a child class.
* 
* @class
* @extends {ParentClass}
*/

import {ParentClass} from "./ParentClass";

class ChildClass extends ParentClass {
  // ChildClass vars and functions here
}

program.ts

import {ChildClass} from "./ChildClass";

let cc = new ChildClass();
cc.parentFunction();

Upon attempting to compile the code, tsc will throw this error:

error TS2339: Property 'parentFunction' does not exist on type 'ChildClass'.

Is there any way to resolve this issue? If there is no way to make the typescript compiler recognize inherited properties, is there some way to make tsc ignore this issue so it can at least finish generating the type definition files and allow me to manually add the missing properties to the corresponding child class type definitions?

1 Answers

You've forgotten to import the other files on the childClass.js and program.js. It should be something like this:


ParentClass.js

/**
* This is the parent class.
*
* @class
*/
export class ParentClass {
  parentFunction(){
    console.log("Running parent function");
  }
}

childClass.js

import { ParentClass } from './ParentClass'

/**
* This is a child class.
*
* @class
* @extends {ParentClass}
*/
export class ChildClass extends ParentClass {
  // ChildClass vars and functions here
}

program.js

import { ChildClass } from './childClass'

let cc = new ChildClass();
cc.parentFunction();
Related