1

This code:

let arrowFn = () => {};
arrowFn.prototype.blah;

throws an error at compile time because arrow function don't have prototypes but typescript doesn't error and happily transpile the code.

Why?

1 Answer 1

1

Here you have built in FUnction type:

interface Function {
    /**
     * Calls the function, substituting the specified object for the this value of the function, and the specified array for the arguments of the function.
     * @param thisArg The object to be used as the this object.
     * @param argArray A set of arguments to be passed to the function.
     */
    apply(this: Function, thisArg: any, argArray?: any): any;

    /**
     * Calls a method of an object, substituting another object for the current object.
     * @param thisArg The object to be used as the current object.
     * @param argArray A list of arguments to be passed to the method.
     */
    call(this: Function, thisArg: any, ...argArray: any[]): any;

    /**
     * For a given function, creates a bound function that has the same body as the original function.
     * The this object of the bound function is associated with the specified object, and has the specified initial parameters.
     * @param thisArg An object to which the this keyword can refer inside the new function.
     * @param argArray A list of arguments to be passed to the new function.
     */
    bind(this: Function, thisArg: any, ...argArray: any[]): any;

    /** Returns a string representation of a function. */
    toString(): string;

    prototype: any;
    readonly length: number;

    // Non-standard extensions
    arguments: any;
    caller: Function;
}

As you see, FUnction.prototype has any type. That's why you don't have any error.

let arrowFn = () => {};

function x(){}


x.prototype
arrowFn.prototype

Both x and arrowFn refering to same Function.prototype type.

If you replace your arrow function with function allowFn(){}, you will receive just undefined, without error

UPDATE

const x = () => { }
const y = function () { }
function z() { }

type X = typeof x // () => void
type Y = typeof y // () => void
type Z = typeof z // () => void

Sign up to request clarification or add additional context in comments.

2 Comments

So, even though arrow functions were added to the JS five years ago, TS doesn't have a proper definition for them but treats them just as if they were standard functions??
@marzelin I made an update, but it seems yes. Maybe smbd will correct me

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.