typed-ember / ember-cli-typescript

Use TypeScript in your Ember.js apps!
https://docs.ember-cli-typescript.com
MIT License
363 stars 99 forks source link

Types of parameters 'controller' and 'controller' are incompatible #1447

Closed lolmaus closed 3 years ago

lolmaus commented 3 years ago

Which package(s) does this problem pertain to?

What are instructions we can follow to reproduce the issue?

I have this class definition:

import Route from '@ember/routing/route';
import Controller from '@ember/controller';

// ToDo: https://github.com/typed-ember/ember-cli-typescript/issues/790
import Transition from '@ember/routing/-private/transition';

export default class RouteCustom extends Route {
  setupController(controller: Controller, model: {}): void {
    super.setupController(controller, model);

    // @ts-ignore
    if (controller.enter) controller.enter();
  }

  resetController(controller: Controller, isExiting: boolean, transition: Transition): void {
    super.resetController(controller, isExiting, transition);

    // @ts-ignore
    if (controller.leave) controller.leave(isExiting);
  }
}

It produces the following error:

Property 'setupController' in type 'RouteCustom' is not assignable to the same property in base type 'Route<unknown>'.
  Type '(controller: Controller, model: {}) => void' is not assignable to type '(controller: Controller, model: unknown, transition: Transition) => void'.
    Types of parameters 'controller' and 'controller' are incompatible.
      Type 'import("/home/lolmaus/Code/kaliber5/tujspk-frontend/node_modules/@types/ember__application/node_modules/@types/ember__controller/index").default' is not assignable to type 'import("/home/lolmaus/Code/kaliber5/tujspk-frontend/node_modules/@types/ember__controller/index").default'.
        Types of property 'queryParams' are incompatible.
          Type 'string | string[] | { [key: string]: { type?: QueryParamTypes | undefined; scope?: QueryParamScopeTypes | undefined; as?: string | undefined; }; }[]' is not assignable to type '(string | Record<string, string | QueryParamConfig | undefined>)[]'.
            Type 'string' is not assignable to type '(string | Record<string, string | QueryParamConfig | undefined>)[]'.ts(2416)

There seems to be a collision between two DefinitelyTyped libraries:

@types/ember__application/node_modules/@types/ember__controller/index
@types/ember__controller/index

One of them is used in my file when I do import Controller from '@ember/controller'.

The other one is used in @ember/routing/route' when it does import Controller from '@ember/controller' (and I do import Route from '@ember/routing/route').

Versions

    "@types/chai": "^4.2.12",
    "@types/chai-as-promised": "^7.1.4",
    "@types/chai-dom": "^0.0.10",
    "@types/chai-subset": "^1.3.3",
    "@types/ember-data": "^3.16.15",
    "@types/ember-data__adapter": "^3.16.1",
    "@types/ember-data__model": "^3.16.2",
    "@types/ember-data__serializer": "^3.16.1",
    "@types/ember-data__store": "^3.16.1",
    "@types/ember-qunit": "^3.4.14",
    "@types/ember-resolver": "^5.0.10",
    "@types/ember__application": "^3.16.3",
    "@types/ember__array": "^3.16.4",
    "@types/ember__component": "^3.16.6",
    "@types/ember__controller": "^3.16.6",
    "@types/ember__debug": "^3.16.4",
    "@types/ember__engine": "^3.16.3",
    "@types/ember__error": "^3.16.1",
    "@types/ember__object": "^3.12.6",
    "@types/ember__polyfills": "^3.12.1",
    "@types/ember__routing": "^3.16.15",
    "@types/ember__runloop": "^3.16.3",
    "@types/ember__service": "^3.16.1",
    "@types/ember__string": "^3.16.3",
    "@types/ember__template": "^3.16.1",
    "@types/ember__test": "^3.16.1",
    "@types/ember__test-helpers": "^2.0.1",
    "@types/ember__utils": "^3.16.2",
    "@types/faker": "^5.5.8",
    "@types/htmlbars-inline-precompile": "^1.0.1",
    "@types/lodash-es": "^4.17.5",
    "@types/qunit": "^2.11.2",
    "@types/rsvp": "^4.0.4",
    "@types/sinon": "^10.0.2",
    "@types/sinon-chai": "^3.2.5",

    "ember-cli": "^3.28.0",
    "ember-cli-typescript": "^4.2.1",
    "typescript": "^4.4.3",

CC @simonihmig

lolmaus commented 3 years ago

There seems to be a similar problem with

import Transition from '@ember/routing/-private/transition';

Type 'Transition' is missing the following properties from type 'Transition': data, promise, catch, finally, and 5 more.ts(2740)

chriskrycho commented 3 years ago

Thanks for the report! This usually happens when you've ended up with multiple copies of the types—it's usually the same basic issue as the error described in our docs at Conflicting Type Dependencies (although the specific error reported is different). Have you tried following the steps on that page?

lolmaus commented 3 years ago

@chriskrycho Thanks, fixing dependency versions with Yarn seemed to help.

But shouldn't all typing packages cross-reference the same versions of themselves? Why are their version numbers out of sync in the first place?

chriskrycho commented 3 years ago

The docs page covers the reasons why: this is a fundamental behavior of the package managers themselves, rather than something to do with us or even the type definitions: https://docs.ember-cli-typescript.com/troubleshooting/conflicting-types

lolmaus commented 3 years ago

This occurs whenever your yarn.lock or package-lock.json files include more than a single copy of a given set of type definitions

@chriskrycho This is not what happened to me. In my case, there's a conflict between the latest version of @types/ember__controller and the latest version of @types/ember__application as they would be installed by the ember-cli-typescript blueprint.

@types/ember__controller is 3.16.6.

@types/ember__application is 3.16.3 which depends on @types/ember__controller version *, which I assume installs the same version as parent, i. e. 3.16.3.

This is something that could be fixed by maintainers of those packages.

chriskrycho commented 3 years ago

That would be very surprising, because the way we set up the dependencies, they are all always installed to latest, and the dependencies on DefinitelyTyped are always set to "*", which make sure they get installed with the latest version initially as well. However, if you update one of those in your own dependencies, it can get out of sync for the reasons described in the docs.

If you can provide a reproduction with a new app or addon where this happens, that would be helpful.

lolmaus commented 3 years ago

Uhm, yes, indeed the issue does not reproduce in a clean install. It probably happened to me because I was upgrading older DefinitelyTyped dependencies by hand.

Interesting: the resolutions entry synced the dependencies, and now I'm able to remove the entry, and the issue is gone.

Thanks for kind support!

chriskrycho commented 3 years ago

Glad to hear it!