Last active
March 26, 2021 13:16
-
-
Save karol-majewski/be21942c7e0fecbd6b4bf772d7e7f199 to your computer and use it in GitHub Desktop.
The difference in property binding order between TypeScript and Babel
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
constructor(public namespaces: string[]) { | |
this.namespaces = namespaces; | |
} | |
options = { | |
ns: this.namespaces | |
}; | |
get opts() { | |
return ({ | |
ns: this.namespaces | |
}) | |
} | |
} | |
console.log( | |
new Foo([]).namespaces, // [] | |
/** | |
* TypeScript: [] | |
* Babel: undefined | |
*/ | |
new Foo([]).options.ns, | |
new Foo([]).opts.ns | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Foo { | |
public namespaces: string[]; | |
constructor(namespaces: string[]) { | |
this.namespaces = namespaces; | |
} | |
options = { | |
// Compile-time error (as it should) | |
// Property 'namespaces' is used before its initialization.(2729) | |
ns: this.namespaces | |
}; | |
get opts() { | |
return ({ | |
ns: this.namespaces | |
}) | |
} | |
} | |
console.log( | |
new Foo([]).namespaces, // [] | |
new Foo([]).options.ns, // undefined | |
new Foo([]).opts.ns // [] | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment