gpt4 book ai didi

typescript - 使用 'as Movie[]' 而不是 '' @typescript-eslint/consistent-type-assertions

转载 作者:行者123 更新时间:2023-12-04 09:10:16 31 4
gpt4 key购买 nike

我有一个 Vuex 商店:

type Movie = {
title: string;
id: number;
}

export default new Vuex.Store({
state: {
searchList: <Movie[]>[],
},
当我编译我的代码时,我收到一个错误:

Use 'as Movie[]' instead of '<Movie[]>' @typescript-eslint/consistent-type-assertions


我不清楚正确的语法应该是什么。
我试过了:
export default new Vuex.Store({
state: {
searchList as Movie[]
},
但这会返回:

No value exists in scope for the shorthand property 'searchList'. Either declare one or provide an initializer.

最佳答案

它要求您使用 as type assertion指定 [] 的类型.
使用的原因之一 asas在 TSX 文件中工作(因此规则名称中的“一致”)。

export default new Vuex.Store({
state: {
searchList: [] as Movie[],
},
如果您不使用 TSX 并且更喜欢尖括号语法,您可以考虑更新您的 linter 规则。

关于typescript - 使用 'as Movie[]' 而不是 '<Movie[]>' @typescript-eslint/consistent-type-assertions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63363586/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com