I'm trying to create my own types on which i can call functions using the dot syntax
.
for example:
let myOwnType: myByteType = 123211345;
myOwnType.toHumanReadable(2);
I want to archive the same behavior like number, array etc. I don't want to create my type using the call signature or constructor signature
So after looking into the typescript libary i saw this number interface:
interface Number {
/**
* Returns a string representation of an object.
* @param radix Specifies a radix for converting numeric values to strings. This value is only used for numbers.
*/
toString(radix?: number): string;
/**
* Returns a string representing a number in fixed-point notation.
* @param fractionDigits Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
*/
toFixed(fractionDigits?: number): string;
/**
* Returns a string containing a number represented in exponential notation.
* @param fractionDigits Number of digits after the decimal point. Must be in the range 0 - 20, inclusive.
*/
toExponential(fractionDigits?: number): string;
/**
* Returns a string containing a number represented either in exponential or fixed-point notation with a specified number of digits.
* @param precision Number of significant digits. Must be in the range 1 - 21, inclusive.
*/
toPrecision(precision?: number): string;
/** Returns the primitive value of the specified object. */
valueOf(): number;
}
The Problem is that i could not find where and how the function bodys gets defined. I was thinking there must be a class implementing the interface or something like this.
I came up trying something like this:
interface Bytes {
toHumanReadable(decimals: number): bytesToHumanReadable;
bytesAs(unit: string): string;
}
function bytesToHumanReadable (decimals: number) {
// convert bytes to GB, TB etc..
}
How could i create my own types and use them just like a normal type? I know that the Interface doesn't work either but it was my first guess.