-
Notifications
You must be signed in to change notification settings - Fork 12.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added error for class properties used within their own declaration #29395
Added error for class properties used within their own declaration #29395
Conversation
Fixes microsoft#5987. Usages of a class property in a preceding property already gave an error, but the following doesn't yet: ```ts class Test { x: number = this.x; } ``` As with other use-before-declare checking, IIFEs are not treated as invalid uses.
Ping @sandersn, any chance this could be reviewed please? 😄 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- Need to flip the boolean return value.
- Need to merge from master.
- How is the code in isInPropertyInitializer used?
Otherwise looks like exactly the thing.
Hm, build still fails on tests/cases/compiler/classUsedBeforeInitializedVariables.ts. Does this need another merge from master? |
Hey folks, a quick note here: we've been relying on this behavior as a workaround for using classes in older versions of Ember (<3.6), and I don't think there's (guaranteed to be?) a TDZ here. The current spec doesn't mention one, and in the case of subclassing it's valid: class Foo {
baz = "hi"
}
class Bar extends Foo {
baz = this.baz !== undefined ? this.baz : "hello"
}
let bar = new Bar();
bar.baz; // "hi" The error correctly doesn't come up when subclassing (as in this example)… but not all instances of subclassing (where this is valid) can be made transparent to TS Both Chrome's shipped and Firefox's feature-flagged implement the feature that way ☝️, as does the Babel implementation, and Ember's legacy class construction behaved in such a way as to require something like that (or some other workaround) when interoperating ES classes with its legacy classes. Edit: to elaborate, in Ember versions prior to 3.6, the instance would already have a value set on it in certain cases courtesy of the implicit |
@chriskrycho can you give an example of the legacy class + ES class failure? I guess that it's something like class Bar extends LegacyFooGen() {
baz = this.baz + 1 // overrides LegacyFooGen().baz, but Typescript doesn't know that.
} Maybe we can restrict the scope of this error to avoid pre-3.6 Ember. |
Yes! I should have done that in the first comment; sorry. The common pattern looks like this: import Component from '@ember/component';
import defaultTo from 'lodash/default-to';
export default class Welcome extends Component {
greeting: string = defaultTo(this.greeting, "Hello");
} (Most folks doing this are using <Welcome />
<Welcome @greeting="Bonjour" /> Prior to Ember 3.6, the constructor for |
Just curious, is the fact that some uses of |
@weswigham and I thought of two fixes, which we would probably want together:
I think this is usually a good error, so I'd prefer it to show up most of the time. However, strictPropertyInitialisation behaves as if every property declaration had a postfix- I'll create an issue and we can talk about it at a design meeting. |
Yeah, and there's currently no type data emitted from that template context into the TS context. This is on the Ember TypeScript team's radar, but not implemented yet. (I looked at type workarounds, but unfortunately, because component args are just smooshed into the component directly, none of them work.) I think that suggested |
Chiming in here (I originally discovered this issue), I think the proposed solutions make sense and I'm glad there will be a way to squelch the error on a case-by-case basis (with |
Fixes #5987.
Usages of a class property in a preceding property already gave an error, but the following doesn't yet:
As with other use-before-declare checking, IIFEs are not treated as invalid uses.