ts里 这个interface表示什么意思

看教程看到了这么一个教程

interface Interface {
    names:string
    (arg:number):string
}

这个怎么解释? 一个有name 属性 返回类型为string 的函数?

阅读 4.4k
2 个回答
interface I {
    (): string;
    [x: string]: number;
}
const foo: I = Object.assign(
    // Callable signature implementation
    () => 'hi',
    {
        // Additional properties
        text2: 3
    }
)

转自 Stack Overflow src

  1. interface 可以描述一个函数或者对象, 上面是描述函数类型
  2. names: string, names 为必要属性
  3. 函数接受参数类型为 number, 参数名为 arg 的参数;
  4. 返回一个类型为 string 的值
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进