emberjs / ember.js

Ember.js - A JavaScript framework for creating ambitious web applications
https://emberjs.com
MIT License
22.45k stars 4.21k forks source link

`init` in classic components takes an argument in the stable types #20483

Open boris-petrov opened 1 year ago

boris-petrov commented 1 year ago

🐞 Describe the Bug

init in classic components used to not accept any arguments and super.init() could be called like that. Since 5.1.0, this no longer works. I believe this is a bug.

app/components/foo-component.ts:5:11 - error TS2554: Expected 1 arguments, but got 0.

5     super.init();
            ~~~~~~

  node_modules/ember-source/types/stable/@ember/-internals/glimmer/lib/component.d.ts:786:10
    786     init(properties: object | undefined): void;
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    An argument for 'properties' was not provided.

🔬 Minimal Reproduction

Repo.

Run npm install and then ./node_modules/.bin/tsc.

😕 Actual Behavior

Type error on super.init().

🤔 Expected Behavior

No error. Removing the last commit (which upgrades Ember) and rerunning npm install and then tsc will remove the errors.

🌍 Environment

cc @chriskrycho

chriskrycho commented 1 year ago

Whoops, this one is quite straightforward and I missed it while doing the work for 5.1.1. Should be fixed by #20492.

boris-petrov commented 1 year ago

Thanks for the release! However, even with 5.1.2 I get the same error. The init method comes from node_modules/ember-source/types/stable/@ember/object/core.d.ts:114. There is a comment on that noting:

NOTE: If you do override `init` for a framework class like `Component`
from `@ember/component`, be sure to call `this._super(...arguments)`
in your `init` declaration!
If you don't, Ember may not have an opportunity to
do important setup work, and you'll see strange behavior in your
application.

So I'm not sure whether we really need to pass ...arguments or not... I was under the impression that this is not needed? Is this some old comment or it's still true?

boris-petrov commented 1 year ago

I pushed another commit to the reproduction repo. It simply does:

import Controller from '@ember/controller';

export default class ApplicationController extends Controller {
  public override init(): void {
    super.init();
  }
}

That causes an error. That is, super.init() in components now works fine but in controllers (and routes, and services, and classes that simply extend EmberObject) doesn't. The error is:

app/controllers/application.ts:5:11 - error TS2554: Expected 1 arguments, but got 0.

5     super.init();
            ~~~~~~

  node_modules/ember-source/types/stable/@ember/object/core.d.ts:114:10
    114     init(_properties: object | undefined): void;
                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    An argument for '_properties' was not provided.
chriskrycho commented 1 year ago

Will take a gander sometime this week or next to try to get this resolved. Thanks for the follow-up!