Typescript: Arrow function: Block-scoped variable used before its declaration

Created on 23 Mar 2017  路  5Comments  路  Source: microsoft/TypeScript

It's not possible to use arrow functions + inheritance anymore.

More info: https://basarat.gitbooks.io/typescript/content/docs/arrow-functions.html#tip-arrow-functions-and-inheritance



TypeScript Version: 2.2.1 / nightly (2.2.0-dev.201xxxxx)

Code

// A *self-contained* demonstration of the problem follows...
class Base {
    f = () => {
        console.log("one");
    }
}

class Child extends Base {
    super_f = this.f;
    f = () => {
        this.super_f();
        console.log("two");
    }
}

let c = new Child();
c.f();

Expected behavior:

Should output

one
two

Actual behavior:

index.ts(8,20): error TS2448: Block-scoped variable 'f' used before its declaration.
Bug Fixed

Most helpful comment

@basarat why did you write this down 馃槩 馃槶

All 5 comments

@basarat why did you write this down 馃槩 馃槶

Extra check we need is "You are allowed to access your own properties before they are initialized as long as your base class declares them"

why did you write this down 馃槩 馃槶

sowee :rose: but they asked how : https://github.com/basarat/typescript-book/issues/125#issue-163820339 :heart:

This bug is also present in 2.2.2. I am also trying to use arrow functions inheritance and it fails with:
"Block-scoped variable '**' used before its declaration."

Yes,I use the version 2.2.2 and I have the same issue

Was this page helpful?
0 / 5 - 0 ratings

Related issues

seanzer picture seanzer  路  3Comments

wmaurer picture wmaurer  路  3Comments

Antony-Jones picture Antony-Jones  路  3Comments

siddjain picture siddjain  路  3Comments

zhuravlikjb picture zhuravlikjb  路  3Comments