m.route.Link type issue
erikvullings opened this issue · 1 comments
erikvullings commented
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 commented
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')