Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

m.route.Link type issue #40

Open
erikvullings opened this issue Aug 16, 2019 · 1 comment
Open

m.route.Link type issue #40

erikvullings opened this issue Aug 16, 2019 · 1 comment

Comments

@erikvullings
Copy link

When writing the following code

return m(isExternalLink() ? 'a' : m.route.Link, { href: '' }, 'Link text')

The following exception is raised:

const title: string | m.Vnode<any, any>
Argument of type 'Component<RouteLinkAttrs, {}> | "a"' is not assignable to parameter of type 'ComponentTypes<RouteLinkAttrs, {}>'.
  Type '"a"' is not assignable to type 'ComponentTypes<RouteLinkAttrs, {}>'.ts(2345)

while this is perfectly legal (and working) JS code.

See also gitter for the original discussion of the behaviour.

@spacejack
Copy link
Collaborator

spacejack commented Aug 16, 2019

Hi @erikvullings, thanks for the report. I'm not sure if there's a good solution here. The hyperscript signature is defined with several overloads rather than as a single function accepting union types. Here's a standalone example of the problem:

let b: boolean = true
function f(a: string): void
function f(n: number): void
function f(a: any): void {}
f(b ? 'a' : 1) // <-- won't compile

We could change to a single, unified hyperscript signature that accepts union types for all the parameters, but the downside is that we'd lose almost all type checking (for component attrs, children.)

For your case, I would instead use:

return isExternalLink() ? m('a', {href: ''}, 'Link text') : m(m.route.Link, {href: ''}, 'Link text')

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants