javascripttypescript.d.ts

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:

// 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 {};
    }