【问题标题】:Typescript typecasting error while using vue.js使用 vue.js 时打字稿类型转换错误
【发布时间】:2018-08-12 01:22:49
【问题描述】:

使用 vue.js 时出现 Typescript 类型转换错误

export interface User {
  id: number
  uuid: string
  email: string
  name: string
  role: number
}

let myObject: User
let otherObject: any
myObject = otherObject as User

错误代码

  http://eslint.org/docs/rules/  Parsing error: Unexpected token, expected ";"
  46 |         let otherObject: any
  47 |         // values are assigned to them, and...
> 48 |         myObject = otherObject as User    // 
  src\components\modal\LoginModal.vue:81:31
          myObject = otherObject as User

使用的ide是vscode

我不知道为什么会出现这个错误。

这是.. 使用 typescript 和 tslint 版本

版本:typescript 2.7.2,tslint 5.9.1

【问题讨论】:

标签: typescript vue.js vuejs2


【解决方案1】:

TypeScript interfaces 的成员必须用分号分隔:

export interface User {
  id: number;
  uuid: string;
  email: string;
  name: string;
  role: number;
}

let myObject: User;
let otherObject: any;
myObject = otherObject as User;

此外,建议您始终在语句/行的末尾添加分号,即使它们在 JavaScript 中是可选的。

我还注意到它显示eslint 错误,请确保您使用的是tslint 而不是eslint

【讨论】:

  • 谢谢...但是,我的vscode使用自动重新格式化...和分号自动删除..我发现只有在
  • 添加分号时.. eslint show => 4:13 不必要的分号分号 i
  • @GyuhaShin 那么你使用的是 eslint 还是 tslint?尝试禁用无分号规则。
  • 注意,Typescript 团队正在迁移到 ESLint,请参阅 github.com/Microsoft/TypeScript/issues/29288
猜你喜欢
  • 2018-12-17
  • 2018-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-13
  • 2016-03-06
  • 2020-01-17
  • 2017-08-04
相关资源
最近更新 更多