Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JSDoc for class method, overloading, adding multiple signatures.
/**
* @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
@wmhilton

This comment has been minimized.

Copy link

@wmhilton wmhilton commented Jan 31, 2020

This doesn't typecheck correctly in TypeScript.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.