【问题标题】:Typescript generic extends show error message. (unexpected token, ...)Typescript 通用扩展显示错误消息。 (意外的标记, ...)
【发布时间】:2021-03-22 22:42:23
【问题描述】:

我正在尝试扩展 Union 类型。但是,它显示了一条错误消息。

在其他项目中没有问题。

有人知道这个错误吗?

type Foobar = 'foo' | 'bar';

interface OptionProps<T extends Foobar> {
                        ~~~~~~~ // Parsing error: Unexpected token, expected ","
                                   1 | type Fb = 'foo' | 'bar';
                                   2 |
                                 > 3 | interface OptionProps<T extends Fb> {
                                     |                         ^
                                   4 |   value: T;
                                   5 |   setValue: (value: T) => void;
                                   6 | }eslint

  value: T;
  setValue: (value: T) => void;
}

【问题讨论】:

标签: typescript


【解决方案1】:

eslintrc.json 中检查您是否将parser 设置为:

"parser": "@typescript-eslint/parser",

如果设置正确,只需删除 node_modules 并运行 npm install

rm -rf node_modules/
npm install

【讨论】:

    【解决方案2】:

    使用时没有发现错误

    type Foobar = 'foo' | 'bar';
    
    interface OptionProps<T extends Foobar> { 
      value: T;
      setValue: (value: T) => void;
    }
    

    TypeScript Playground

    尝试重新编译您的代码

    【讨论】:

      猜你喜欢
      • 2011-07-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-13
      • 2020-06-09
      • 1970-01-01
      相关资源
      最近更新 更多