简体   繁体   English

TS2364:赋值表达式的左侧无效

[英]TS2364: Invalid left-hand side of assignment expression

a is a mutable array, so why can't I assign to it? a是一个可变数组,为什么我不能分配给它?

foo.ts foo.ts

export let a: any[];

bar.ts bar.ts

import {a} from './foo';

a = [5,6,7];

a contains a mutable array, so you can mutate it: a包含一个可变数组,所以你可以改变它:

a.splice(0, a.length, 5, 6, 7);
console.log(a);

a in bar is an imported binding however, and those cannot be reassigned. abar被导入的结合然而,这些不能被重新分配。 That's why it's a syntax error. 这就是为什么它是一个语法错误。

You can assign to a in foo however, where it's just behaving like a normal variable. 您可以分配到afoo然而,如果它只是表现得像一个正常的变量。

If you want to be able to change the value of the array inside the original scope ( foo.ts ) then you can do something like: 如果您希望能够在原始范围( foo.ts )中更改数组的值,那么您可以执行以下操作:

export var a = {
    arr: []
}

and then in bar.ts : 然后在bar.ts

import {a} from './foo';

a.arr = [5,6,7];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Angular 错误-“赋值表达式的左侧可能不是可选属性 access.ts”? - Angular error- "The left-hand side of an assignment expression may not be an optional property access.ts"? 赋值表达式的左侧必须是变量或属性访问 - The left-hand side of an assignment expression must be a variable or a property access 错误:ReferenceError:分配中的左侧无效 - Error: ReferenceError: Invalid left-hand side in assignment 赋值表达式的左侧在Angular2中不能为常量或只读属性 - Left-hand side of assignment expression cannot be a constant or a read-only property in Angular2 我正在制作图像轮播,但出现错误:赋值表达式的左侧可能不是可选的属性访问 - i'm making a carousel of images and i've the error : The left-hand side of an assignment expression may not be an optional property access 尝试初始化嵌套数组时出错“赋值表达式的左侧可能不是可选的属性访问。” - Getting error when trying to initialize nested array "The left-hand side of an assignment expression may not be an optional property access." 打字稿左手运算符无效 - Typescript Invalid left-hand operator “in”表达式的左侧必须是私有标识符或类型为“any”、“string”、“number”或“symbol” - The left-hand side of an 'in' expression must be a private identifier or of type 'any', 'string', 'number', or 'symbol' 错误 TS2362:算术运算的左侧必须是 'any'、'number'、'bigint' 类型(在字符串插值 {{ }} 内) - Error TS2362: The left-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' (inside string interpolation {{ }}) 'for...in' 语句的左侧不能使用类型注释 - The left-hand side of a 'for...in' statement cannot use a type annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM