What is “type '{}'”?

前端 未结 3 1363
我寻月下人不归
我寻月下人不归 2020-12-11 14:45

In TypeScript, what exactly is type \'{}\' and how does it relate to other built-in types?

For example, the last line of the following example gives

3条回答
  •  Happy的楠姐
    2020-12-11 15:08

    You need to have your call function like this:

    function call(fn: NoArgsFn): T {
        return fn();
    }
    

    In your original call function you had 2 generic types which you did not pass when calling the function and the compiler failed to infer what types they are.


    Edit

    Type {} is an object literal (as far as I'm aware), and so you can face the same error like this:

    var o = {};
    var n: number = o; // Error: Type '{}' is not assignable to type 'number'
    

    I'm not exactly sure why the compiler inferred that the function returns {} in your example.

提交回复
热议问题