aurelia / router

A powerful client-side router.
MIT License
120 stars 115 forks source link

aurelia-cli beta 1.7/typescript 3.2.2 activationStrategy not allowed in router.config #628

Open jeremy-holt opened 5 years ago

jeremy-holt commented 5 years ago

I'm submitting a bug report

Please tell us about your environment:

Current behavior: Just ran yarn upgrade. It updated aurelia-cli from beta 5 to to beta 7

au build fails to compile on route.config[map ..... activationStrategy:'replace".....

Expected/desired behavior: It should compile

3cp commented 5 years ago

Can you show more details? It looks like a syntax error 'replace".

jeremy-holt commented 5 years ago

Sorry - that was my bad typing. This was the actual code. I removed activationStrategy and it compiles now.

 public configureRouter(config: RouterConfiguration, router: Router) {
    config.title = "Amberwood";

    config.map([
      { route: ["", "home"], moduleId: PLATFORM.moduleName("modules/home/home"), name: "home", nav: true, title: "Home" },
      { route: "client/:id?", href: "#/client", moduleId: PLATFORM.moduleName("modules/client/client-edit"), name: "clientEdit", nav: true, title: "Clients" },
      {
        route: "contract/:id?", href: "#/contract", moduleId: PLATFORM.moduleName("modules/contract/contract-edit"), name: "contractEdit", nav: true, title: "Contracts",
        activationStrategy: "replace"
      },
      { route: "contract/print", moduleId: PLATFORM.moduleName("modules/printContract/print-contract"), name: "printContract", nav: false, title: "Print contract" },
      {
3cp commented 5 years ago

Sorry, I am not TS user. But can you try TS v3.1.6 + cli beta7? It could be the same reason for the bluebird import failure.

jeremy-holt commented 5 years ago

I suspect it was the upgrade to TS 3.2.2 - Stupidly I did them both at the same time. Each time I upgrade TS it finds more errors in my code (good thing!). I do remember that putting in activationStrategy:"replace" never worked anyway. I ended up changing the query string when wanting to reload the same page. I should have removed activationStrategy from route.config at the time.

3cp commented 5 years ago

Based on the d.ts file, I guess you need to do

import {activationStrategy} from 'aurelia-router';
... activationStrategy: activationStrategy.replace,
jeremy-holt commented 5 years ago

Nope - doesn't like that

image

jeremy-holt commented 5 years ago
[ts]
Argument of type '({ route: string[]; moduleId: string; name: string; nav: boolean; title: string; } | { route: string; href: string; moduleId: string; name: string; nav: boolean; title: string; } | { route: string; href: string; ... 4 more ...; activationStrategy: string; } | { ...; } | { ...; } | { ...; })[]' is not assignable to parameter of type 'RouteConfig | RouteConfig[]'.
  Type '({ route: string[]; moduleId: string; name: string; nav: boolean; title: string; } | { route: string; href: string; moduleId: string; name: string; nav: boolean; title: string; } | { route: string; href: string; ... 4 more ...; activationStrategy: string; } | { ...; } | { ...; } | { ...; })[]' is not assignable to type 'RouteConfig[]'.
    Type '{ route: string[]; moduleId: string; name: string; nav: boolean; title: string; } | { route: string; href: string; moduleId: string; name: string; nav: boolean; title: string; } | { route: string; href: string; ... 4 more ...; activationStrategy: string; } | { ...; } | { ...; } | { ...; }' is not assignable to type 'RouteConfig'.
      Type '{ route: string; href: string; moduleId: string; name: string; nav: boolean; title: string; activationStrategy: string; }' is not assignable to type 'RouteConfig'.
        Types of property 'activationStrategy' are incompatible.
          Type 'string' is not assignable to type '"replace" | "no-change" | "invoke-lifecycle"'. [2345]
jeremy-holt commented 5 years ago

OK - so it's just a problem in the d-ts. Need to change it to activationStrategy: "replace" | "no-change" | "invoke-lifecycle"

3cp commented 5 years ago

@bigopon @davismj some typing problem with latest TS?

jeremy-holt commented 5 years ago

Sorry - misspoke. It is correct in the d.ts. Can't see why it throws an error for 'replace'.

davismj commented 5 years ago

The router typings are correct. This is a bug or a quirk, depending on how positive you are, in TypeScript itself. See https://github.com/Microsoft/TypeScript/issues/10570.

config.map([
  { route: 'foo', moduleId: 'bar', activationStrategy: 'invoke-lifecycle' }
]);

What happens is that when you write the above TypeScript infers a type from the route config object literal and then tries to match it up with RouteConfig. In the first step, it recognizes 'invoke-lifecycle' as a string and not a string literal type, which is incompatible with the string literal type.

Workaround

config.map([
  { route: 'foo', moduleId: 'bar', activationStrategy: 'invoke-lifecycle' },
  { route: 'foo', moduleId: 'bar', activationStrategy: 'invoke-lifecycle' }
] as RouteConfig[]);

For now, this will work. You could also cast just the activationStrategy: <'invoke-lifecycle'>'invoke-lifecycle' or the individual route configs { ... } as RouteConfig,

Fix

We need to drop the string literal typing in favor of either a string or an enum-like.

EisenbergEffect commented 5 years ago

I think this was fixed in the latest version of TS, wasn't it?

davismj commented 5 years ago

Haven't seen that. Issue is still open in MS repo and upgrading to latest is what brought my attention to this issue.

ejuntu commented 5 years ago

I had this issue and got around with var replaceStrat: any = "replace"; config.map([ { route: "", name: "index", moduleId: PLATFORM.moduleName("pages/index/index", "index"), title: "Front page", nav: 0, activationStrategy: replaceStrat },