【问题标题】:Cannot invoke an expression whose type lacks a call signature. Type has no compatible call signatures无法调用其类型缺少调用签名的表达式。类型没有兼容的调用签名
【发布时间】:2017-05-01 23:34:02
【问题描述】:

我正在尝试为 docuri 定义

export type URI<K extends RouteParams> = string;

export interface RouteParams {
  [key: string]: (string | number | boolean)
}

export interface Document {
  [key: string]: (string | number | boolean)
}

/**
 * Create a URI from a document properties
 * @param the props to build the URI from
 * @return the URI
 */
export type RouteCreator<K extends RouteParams> = (props: K) => string;

/**
 * Parses a URI and returns the props
 * @param uri the URI to parse
 * @return the params parsed from URI
 */
export type RouteParser<K extends RouteParams> = (uri: string) => K;

export type Route<T extends RouteParams> = RouteParser<T> | RouteCreator<T>;

/**
 * Creates a Route which is a function that either parse or stringify object/string
 * @param route the route uri
 * @return the Route
 */
export type RouteFactory<K extends RouteParams> = (route: string) => Route<K>;

export interface DocURI<K extends RouteParams> {
  route: RouteFactory<K>;
}

然后使用它:

import {DocURI, Document, RouteParams, URI, RouteFactory} from './Definitions';

const docuri = require('docuri');

function getRoute <T extends Document> (): DocURI<T> {
  return (docuri as DocURI<T>);
}
...
const artistURI = getRoute<ArtistParams>().route('artist/name');

const parsed = artistURI(album.artist); // Cannot invoke an expression whose type lacks a call signature. Type 'Route<ArtistParams>' has no compatible call signatures.

我在最后一行:

 Cannot invoke an expression whose type lacks a call signature. Type 'Route<ArtistParams>' has no compatible call signatures.

我做错了什么?

【问题讨论】:

    标签: typescript


    【解决方案1】:

    查看这个问题:Call signatures of union types:

    这是目前的设计,因为我们不合成 获取联合类型的成员时的交叉调用签名 -- 只有相同的调用签名出现在联合类型上

    所以现在您需要使两个函数签名相同,或者转换结果:

    const artistURI = getRoute<ArtistParams>().route('artist/name') as (str: string) => string;
    
    const parsed = artistURI(album.artist); // should be ok
    

    【讨论】:

    • 谢谢!现在我只是把any =&gt; any作为签名
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-26
    • 1970-01-01
    • 2017-10-11
    • 2020-05-17
    • 2019-10-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多