【问题标题】:Typescritp syntax for default export默认导出的打字稿语法
【发布时间】:2021-02-07 00:27:36
【问题描述】:

我是打字稿的新手。

在这段代码中

import { ITokenCache } from './tokens/token-cache';
import { ISession } from './session/session';
import { ISignInWithAuth0 } from './instance';

export default function createDummyBrowserInstance(): ISignInWithAuth0 & { isBrowser: boolean } {
  return {
    isBrowser: true,
    handleLogin: (): Promise<void> => {
      throw new Error('The handleLogin method can only be used from the server side');
    },
    handleLogout: (): Promise<void> => {
      throw new Error('The handleLogout method can only be used from the server side');
    },
    handleCallback: (): Promise<void> => {
      throw new Error('The handleCallback method can only be used from the server side');
    },
    handleProfile: (): Promise<void> => {
      throw new Error('The handleProfile method can only be used from the server side');
    },
    getSession: (): Promise<ISession | null | undefined> => {
      throw new Error('The getSession method can only be used from the server side');
    },
    requireAuthentication: () => (): Promise<void> => {
      throw new Error('The requireAuthentication method can only be used from the server side');
    },
    tokenCache: (): ITokenCache => {
      throw new Error('The tokenCache method can only be used from the server side');
    }
  };
}

下面这行我看不懂。

我知道这是默认导出,但不明白函数名后面的语法。

export default function createDummyBrowserInstance(): ISignInWithAuth0 & { isBrowser: boolean } {

你能帮忙吗?

【问题讨论】:

标签: typescript


【解决方案1】:

这是返回类型。

它使用intersection types。在这种情况下,它返回一个类型为 ISignInWithAuth0 的对象,并带有一个附加的布尔属性。

我建议您阅读/浏览链接的文档。

【讨论】:

  • 函数 createDummyBrowserInstance 也返回一个对象。所以导出的最终对象是 ISignInWithAuth0 & { isBrowser: boolean } & object from return 的组合。
  • 不是。函数名和列之后的语法是返回类型。返回的对象需要尊重返回类型,并被键入为返回类型。
猜你喜欢
  • 2017-02-06
  • 2016-01-23
  • 2016-02-10
  • 2019-12-11
  • 2018-04-17
  • 2017-09-23
  • 2020-05-17
  • 2021-11-24
  • 1970-01-01
相关资源
最近更新 更多