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

How to check if a parameter changed in a ".d.ts" File

I dont know if this question once was asked, because I cant find it anywhere. (maybe I’m blind)
If this question is a dup, please tell me as this might also fixes my problem.

I want to change the return inside my jsDocs to match what really returns and not array | object.

Here an Example:

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

// class.someFunc: (str: string, bool?: boolean) => array
class.someFunc('string', true);

// class.someFunc: (str: string, bool?: boolean) => object
class.someFunc('string', false);

I tried with different types of defaults, thought it might change.

Both in the .d.ts function and in the jsDocs above.

/** @param [bool=true] */
static someFunc(str: string, bool: boolean=true): array;

/** @param [bool=false] */
static someFunc(str: string, bool: boolean=false): object;

But it still used the above definition from the .d.ts even when bool was false.

>Solution :

If I’m understanding correctly, you’re looking to define typescript overloads

// these are the overloads that can actually be used for typing
function someFunc(str: string, bool: true): object;
function someFunc(str: string, bool: false): any[];
// this one is the real implementation, note that its inputs and 
// outputs encompass all of the overloads' types
function someFunc(str: string, bool: boolean): object | any[] {
  if (bool) return [];
  return {};
}
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