Skip to content

Interesting quirk, should be aware of #27

@getify

Description

@getify

There's nothing to do for this issue... I just want to document this quirk somewhere.

var x;
foo(x);
x = 3;
foo(x);
function foo(a = number) {}

Think carefully about this code. What do you expect to happen? Any errors?

This code requires a second pass, because of the foo(..) hoisting. On the first pass, x is initially implied as undef, then it's re-implied as number. So... on the second pass, would we expect the first foo(x) to throw an error or not?

I claim: yes it should throw an error. Even though x becomes number later in the code, at the moment we first see foo(x), it's still undef. On that second pass, we know foo(..) expects a number, so we should get an error about argument type mismatch.

Likewise, for the second foo(x)... since that one comes after a = 3, by that moment (even on the first pass), we know that foo(x) is trying to pass a number. So... since that's valid, no error should be thrown for that line.

The output (currently) is:

(pass 1) ------------------------
Implying x as inferred-type 'undef', at line 1, column 4
Re-implying x to inferred-type 'number', at line 3, column 0
Implying foo as inferred-type 'func', at line 5, column 0
Implying a as tagged-type 'number', at line 5, column 13
Function 'foo' signature: {"type":"func","params":[{"tagged":"number"}],"hasRestParam":false,"return":{"default":true,"inferred":"undef"}}, at line 5, column 0
(pass 2) ------------------------
Argument type mismatch: expected type 'number', but found type 'undef', at line 2, column 4
Function 'foo' signature: {"type":"func","params":[{"tagged":"number"}],"hasRestParam":false,"return":{"default":true,"inferred":"undef"}}, at line 5, column 0

Metadata

Metadata

Assignees

No one assigned

    Labels

    cool-exampleSomething very interesting to look at

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions