Last active
September 23, 2022 18:01
-
-
Save seahindeniz/4de7ad774c1043ddb47b4080667bb06a to your computer and use it in GitHub Desktop.
JSDoc for class method, overloading, adding multiple signatures.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @typedef {(x: number) => string} NumberToStringType | |
* | |
* @typedef {(x: string) => number} StringToNumberType | |
* | |
* @typedef {NumberToStringType & StringToNumberType} FlipType | |
*/ | |
class SampleClass { | |
constructor() { | |
/** | |
* @type {FlipType} | |
*/ | |
this.flip = this._flip; | |
} | |
/** | |
* @type {FlipType} | |
* The @type definition will not effect here | |
*/ | |
_flip(x) { | |
if (typeof x === 'string') | |
return Number(x); | |
else | |
return String(x); | |
} | |
} | |
let sample = new SampleClass(); | |
let number1 = sample.flip(1); // string | |
let string1 = sample.flip("1"); // number | |
let number2 = sample._flip(2); // string | number | |
let string2 = sample._flip("2"); // string | number | |
/** | |
* Arrow function | |
* | |
* @type {FlipType} | |
*/ | |
const flipper = x => { | |
if (typeof x === 'string') | |
return Number(x); | |
else | |
return String(x); | |
} | |
let number3 = flipper(3); // string | |
let string3 = flipper("3"); // number |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It would be nice to hae this feature working without hacking the class prototype....