admin管理员组文章数量:1323157
When I rename a variable in JavaScript or TypeScript, VS Code sometimes adds aliases in destructured assignments:
const { renamedProp: prop } = arg; // After rename
or it adds as
in imports:
import { Foo as renamedFoo } from "./file"; // After rename
Why does VS Code do this and how can I disable this behavior? For example, if I rename prop
in interface Foo
for the following code:
export interface Foo {
prop: string;
}
function bar(arg: Foo) {
const { prop } = arg;
return prop;
}
VS Code changes the code to:
export interface Foo {
renamedProp: string;
}
function bar(arg: Foo) {
const { renamedProp: prop } = arg;
return prop;
}
I want it to be:
export interface Foo {
renamedProp: string;
}
function bar(arg: Foo) {
const { renamedProp } = arg;
return renamedProp;
}
When I rename a variable in JavaScript or TypeScript, VS Code sometimes adds aliases in destructured assignments:
const { renamedProp: prop } = arg; // After rename
or it adds as
in imports:
import { Foo as renamedFoo } from "./file"; // After rename
Why does VS Code do this and how can I disable this behavior? For example, if I rename prop
in interface Foo
for the following code:
export interface Foo {
prop: string;
}
function bar(arg: Foo) {
const { prop } = arg;
return prop;
}
VS Code changes the code to:
export interface Foo {
renamedProp: string;
}
function bar(arg: Foo) {
const { renamedProp: prop } = arg;
return prop;
}
I want it to be:
export interface Foo {
renamedProp: string;
}
function bar(arg: Foo) {
const { renamedProp } = arg;
return renamedProp;
}
Share
asked Mar 19, 2019 at 4:59
Matt BiernerMatt Bierner
65.7k24 gold badges197 silver badges230 bronze badges
1 Answer
Reset to default 12By default, VS Code attempts to make renames safe. This means preserving the existing interfaces of types. In cases like the following example,
export interface Foo {
prop: string;
}
function bar(arg: Foo) {
const { prop } = arg;
return { prop };
}
If we rename prop
without using aliases, the implicitly returned type of bar
would change. And maybe this type was used to satisfy another interface that expects a property called prop
. In this case, introducing an alias on rename preserves the existing type interfaces whichs ensures that the code continues to pile and work as expected
To disabled this behavior, just set:
"javascript.preferences.useAliasesForRenames": false,
"typescript.preferences.useAliasesForRenames": false,
These settings are only supported when using TypeScript 3.4+ in your workspace (this is the default in VS Code 1.33+)
本文标签:
版权声明:本文标题:Stop VS Code from adding 'as' or alias in destructured assignment when renaming in JavaScript or TypeScript - St 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742143744a2422696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论