GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||
GeezerVison is showing github.com — Added error for class properties used within their own declaration by JoshuaKGoldberg · Pull Request #29395 · microsoft/TypeScript Conversation
Contributor
Fixes #5987. Usages of a class property in a preceding property already gave an error, but the following doesn't yet: class Test { x: number = this.x; } As with other use-before-declare checking, IIFEs are not treated as invalid uses.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsFixes 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. Uh oh!
There was an error while loading. Please reload this page.
Klaus Meinhardt (ajafff)
suggested changes
Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
Contributor
Author
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Nathan Shively-Sanders (sandersn)
requested changes
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Otherwise looks like exactly the thing.
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactionsUh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page. Uh oh!
There was an error while loading. Please reload this page.
Nathan Shively-Sanders (sandersn)
approved these changes
Member
All reactions
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page.
Josh Ghoulberg 👻 (JoshuaKGoldberg)
deleted the
property-use-before-declare
branch
|
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Chris Krycho (@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Just curious, is the fact that some uses of |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Wesley Wigham (@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. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
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 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
Dangerous Property Initializers in Classes
There was an error while loading. Please reload this page.