打字稿 - 按值进行自定义类型分配

发布于 2025-01-27 01:51:52 字数 636 浏览 2 评论 0原文

在我的一个过程中,我需要定期重置自定义类型,但我注意到Typescript(也许也可以在JavaScript中),当我将自定义类型的变量分配给同一类型的另一个变量时,分配是通过参考而不是通过价值制作。

例如,此代码:

type testing = {
    a: string;
    b: number;
};

let v1: testing = { a: "one", b: 1 };
let v2: testing = { a: "two", b: 2 };

console.log(v1);
console.log(v2);

v1 = v2;
v1.a = "Edited";

console.log(v1);
console.log(v2);

生成此输出

{ a: 'one', b: 1 }
{ a: 'two', b: 2 }
{ a: 'Edited', b: 2 }
{ a: 'Edited', b: 2 }

可以通过值分配它而不分配类型的每个属性?

(在我的示例中,我需要我的V2变量保持等于{a:“两个”,b:2})

In one of my procedures I need to periodically reset a custom type, but I noticed that in TypeScript (and I think in JavaScript too maybe), when I assign a variable of a custom type to another variable of the same type, the assignment is made by reference and not by value.

For example this code:

type testing = {
    a: string;
    b: number;
};

let v1: testing = { a: "one", b: 1 };
let v2: testing = { a: "two", b: 2 };

console.log(v1);
console.log(v2);

v1 = v2;
v1.a = "Edited";

console.log(v1);
console.log(v2);

generates this output

{ a: 'one', b: 1 }
{ a: 'two', b: 2 }
{ a: 'Edited', b: 2 }
{ a: 'Edited', b: 2 }

Is there a way to assign it by value without assigning every property of the type?

(in my example I need my v2 variable to remain equals to { a: "two", b: 2 })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2025-02-03 01:51:52

对于这样的简单案例,您可以使用传播操作员创建对象的浅副本。

type testing = {
    a: string;
    b: number;
};

let v1: testing = { a: "one", b: 1 };
let v2: testing = { a: "two", b: 2 };

console.log(v1);
console.log(v2);
// copy all of the top-level keys and values from v2 
// into a new object and assign that to v1
v1 = { ...v2 }; 
v1.a = "Edited";

console.log(v1);
console.log(v2);

请注意这里有一些警告。如Tobias的评论中的链接所示,这只会进行浅副本。如果您有更复杂的对象,这将引起问题。

let v1: testing = { a: "one", b: 1, c: { d: 1 } };
let v2: testing = { a: "two", b: 2, c: { d: 2 } };

v2 = { ...v1 };
// v1.c is the same object as v2.c because we only shallowly
// copied, so this assignment is reflected in both
v2.c.d = 60;

console.log(v1);
console.log(v2);

For simple cases like this, you can make use of the spread operator to create a shallow copy of an object.

type testing = {
    a: string;
    b: number;
};

let v1: testing = { a: "one", b: 1 };
let v2: testing = { a: "two", b: 2 };

console.log(v1);
console.log(v2);
// copy all of the top-level keys and values from v2 
// into a new object and assign that to v1
v1 = { ...v2 }; 
v1.a = "Edited";

console.log(v1);
console.log(v2);

Note there are some caveats here. As indicated by the link in Tobias S' comment, this will only do a shallow copy. If you have more complex objects, this will cause issues.

let v1: testing = { a: "one", b: 1, c: { d: 1 } };
let v2: testing = { a: "two", b: 2, c: { d: 2 } };

v2 = { ...v1 };
// v1.c is the same object as v2.c because we only shallowly
// copied, so this assignment is reflected in both
v2.c.d = 60;

console.log(v1);
console.log(v2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文