GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Added error for class properties used within their own declaration by JoshuaKGoldberg · Pull Request #29395 · microsoft/TypeScript

Conversation

@JoshuaKGoldberg

@JoshuaKGoldberg Josh Ghoulberg 👻 (JoshuaKGoldberg) commented Jan 13, 2019

Copy link
Copy Markdown
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 reactions
Josh Goldberg
Added error for class properties used within their own declaration
cd88f6a
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.
JoshuaKGoldberg
Josh Ghoulberg 👻 (JoshuaKGoldberg) commented Jan 13, 2019
View reviewed changes
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

ajafff
Klaus Meinhardt (ajafff) suggested changes Jan 14, 2019
View reviewed changes
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Josh Goldberg
Accepted 'witness' baselines; removed unnecessary !==
38e1856
@RyanCavanaugh Ryan Cavanaugh (RyanCavanaugh) assigned Nathan Shively-Sanders (sandersn) Jan 25, 2019
@JoshuaKGoldberg

Josh Ghoulberg 👻 (JoshuaKGoldberg) commented Apr 2, 2019

Copy link
Copy Markdown
Contributor Author

Ping Nathan Shively-Sanders (@sandersn), any chance this could be reviewed please? 😄

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

sandersn
Nathan Shively-Sanders (sandersn) requested changes Apr 2, 2019
View reviewed changes

@sandersn Nathan Shively-Sanders (sandersn) left a comment

Copy link
Copy Markdown
Member

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.

  1. Need to flip the boolean return value.
  2. Need to merge from master.
  3. How is the code in isInPropertyInitializer used?

Otherwise looks like exactly the thing.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread tests/baselines/reference/classUsedBeforeInitializedVariables.js Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

Josh Goldberg added 3 commits April 3, 2019 08:32
Josh Goldberg
Addressed quick feedback items
3c8ce9b
Josh Goldberg
Merge branch 'master' into property-use-before-declare
ee4e90c
Josh Goldberg
Accepted odd new baseline
6661f90
sandersn
Nathan Shively-Sanders (sandersn) reviewed Apr 3, 2019
View reviewed changes
Comment thread src/compiler/checker.ts Resolved

Uh oh!

There was an error while loading. Please reload this page.

sandersn
Nathan Shively-Sanders (sandersn) approved these changes Apr 3, 2019
View reviewed changes
Josh Goldberg
Fixed post-merge introduced lint errors
236fed9
@sandersn

Nathan Shively-Sanders (sandersn) commented Apr 4, 2019

Copy link
Copy Markdown
Member

Hm, build still fails on tests/cases/compiler/classUsedBeforeInitializedVariables.ts. Does this need another merge from master?

JoshuaKGoldberg reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Josh Goldberg added 2 commits April 4, 2019 14:05
Josh Goldberg
Merge branch 'master' into property-use-before-declare
e767ee0
Josh Goldberg
Updated baselines again
0c832f2
@sandersn
Nathan Shively-Sanders (sandersn) merged commit 0b3b4ea into microsoft:master Apr 5, 2019
@JoshuaKGoldberg
Josh Ghoulberg 👻 (JoshuaKGoldberg) deleted the property-use-before-declare branch April 5, 2019 20:27
@chriskrycho

Chris Krycho (chriskrycho) commented May 30, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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 super() invocation, so it's equivalent to the subclassing example, but in a way that's invisible to TS. (This was a source of considerable pain for us along the way, as you can imagine; this was one of the data points that helped us design a better implementation in 3.6+.)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chriskrycho Chris Krycho (chriskrycho) mentioned this pull request May 30, 2019
Dangerous Property Initializers in Classes #5987
Closed
@sandersn

Nathan Shively-Sanders (sandersn) commented May 30, 2019

Copy link
Copy Markdown
Member

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.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@chriskrycho

Chris Krycho (chriskrycho) commented May 30, 2019 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

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 defaultTo or similar to work correctly with truthy types.) This is to support optional component arguments, so users could do either of these:

<Welcome />
<Welcome @greeting="Bonjour" />

Prior to Ember 3.6, the constructor for Component sets up arguments passed in the template invocation. For 3.6 and later, that gets handled differently by the framework (i.e. not in the Component constructor), so the problem goes away; people can just write greeting = "Hello" as the initializer, and the framework takes care of setting any arguments after construction fully completes.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sandersn

Nathan Shively-Sanders (sandersn) commented May 30, 2019

Copy link
Copy Markdown
Member

Just curious, is the fact that some uses of <Welcome /> have @greeting the only static marker that Welcome will get this.greeting initialised by Component ?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sandersn

Nathan Shively-Sanders (sandersn) commented May 30, 2019

Copy link
Copy Markdown
Member

Wesley Wigham (@weswigham) and I thought of two fixes, which we would probably want together:

  1. Allow this error to be squelched with postfix-!, adding it to the category of property initialisation patterns that are known to be unsafe, but common enough to allow. (There is only one pattern that I know of right now: property!: string;)
  2. Only make this an error with "strictPropertyInitialisation": true.

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-!, so it would make sense for things-squelched-by-! to also be squelched by that flag being false.

I'll create an issue and we can talk about it at a design meeting.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sandersn Nathan Shively-Sanders (sandersn) mentioned this pull request May 30, 2019
Property initialisation check in TS 3.5 is too strict for ember 3.5 #31682
Open
@chriskrycho

Chris Krycho (chriskrycho) commented May 30, 2019

Copy link
Copy Markdown

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 strictPropertyInitialization rule makes sense and would solve this problem. Thanks for the quick response!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@jamescdavis

James C. Davis (jamescdavis) commented May 31, 2019

Copy link
Copy Markdown

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 !) as I don't want to have to disable strictPropertyInitialization globally. Thanks for considering our special needs!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

This was referenced May 31, 2019
Upgrade dependencies CenterForOpenScience/ember-osf-web#678
Closed
[ENG-467] Upgrade dependencies CenterForOpenScience/ember-osf-web#679
Merged
@microsoft Microsoft (microsoft) locked as resolved and limited conversation to collaborators Oct 21, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Reviewers

@sandersn Nathan Shively-Sanders (sandersn) sandersn approved these changes
+1 more reviewer
@ajafff Klaus Meinhardt (ajafff) ajafff requested changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@sandersn Nathan Shively-Sanders (sandersn)

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Dangerous Property Initializers in Classes

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@JoshuaKGoldberg @sandersn @chriskrycho @jamescdavis @ajafff
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.