首页 > 解决方案 > 如何导出可以是对象或布尔值的类型而不会引发未定义的属性错误?

问题描述

嗨,我想创建一个这样的类型:

export type LoginResult = {
    readonly token: string;
    readonly renewal_token: string;
} | boolean;

但是当一个声明一个常量作为这种类型,并且需要访问令牌或更新时,它会抛出:

Property 'token' does not exist on type 'LoginResult'.
  Property 'token' does not exist on type 'false'.ts(2339)

我怎样才能做到这一点 ?有没有其他更好的方法来实现这一目标?谢谢你。

标签: typescripttypesreact-typescript

解决方案


推荐阅读