Skip to content

Instantly share code, notes, and snippets.

@karol-majewski
Last active March 26, 2021 13:16
Show Gist options
  • Save karol-majewski/be21942c7e0fecbd6b4bf772d7e7f199 to your computer and use it in GitHub Desktop.
Save karol-majewski/be21942c7e0fecbd6b4bf772d7e7f199 to your computer and use it in GitHub Desktop.
The difference in property binding order between TypeScript and Babel
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
)
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