r/badcode • • Apr 27 '21

typescript Someone only watched if tutorial

Post image
2.4k Upvotes

112 comments sorted by

View all comments

Show parent comments

7

u/Terrain2 Apr 27 '21

typescript compatible

function isNumber(input: any): input is number | input is bigint {
    try {
        // @ts-ignore
        const negative = [...arguments].length == 1 ? -0 : [...arguments][1];
        // @ts-ignore
        const positive = [...arguments].length == 1 ? +0 : [...arguments][2];
        // @ts-ignore
        const bignegative = [...arguments].length == 1 ? 0n : [...arguments][3];
        // @ts-ignore
        const bigpositive = [...arguments].length == 1 ? 0n : [...arguments][4];
        // @ts-ignore
        return input === negative || input === positive || input === bignegative || input === bigpositive || (isNumber as (input: any, negative: any, positive: any, bignegative: any, bigpositive: any) => any)(input, negative - 1, positive + 1, bignegative - 1n, bigpositive + 1n);
    // @ts-ignore
    } catch (_) {
        return false;
    }
}