Angular+TypeScript:保留let解决TSLint报错及数组操作问题
Hey there! Let's break down your two questions step by step:
let instead of switching to const despite TSLint errors First, let's understand why TSLint is flagging your let keys = []; line: the prefer-const rule triggers when a variable is initialized once and never reassigned later. In your pipe code, you're modifying the array's contents (using push), but you never reassign the keys variable itself (like keys = new Array() or keys = someOtherArray). For reference types like arrays and objects, const only prevents reassigning the variable—it doesn't stop you from modifying the object/array's internal data.
That said, if you still want to keep using let instead of switching to const, here are your options:
- Disable the rule for that specific line: Add a comment right above the
letdeclaration to tell TSLint to ignore this line for theprefer-construle:// tslint:disable-next-line:prefer-const let keys = []; - Turn off the rule globally (not recommended): If you don't want this rule applied anywhere in your project, edit your
tslint.jsonfile and setprefer-consttofalse:
Note: This isn't ideal because the"rules": { "prefer-const": false }prefer-construle helps enforce cleaner, safer code by highlighting variables that don't need to be reassignable.
In TypeScript, there are a few ways to create empty arrays, depending on whether you need type safety or not:
Basic empty array
If you don't need to specify the type of elements upfront (though TypeScript will infer it as you add elements), you can create an array like this:
// Use let if you might reassign the array later (e.g., keys = [1,2,3]) let keys = []; // Use const if you only need to modify the array's contents (not reassign the variable) const keys = [];
Typed empty array (recommended)
For better type safety (a core benefit of TypeScript), define the type of elements your array will hold:
// First, define an interface for your array elements interface KeyValuePair { key: string; value: any; } // Create a typed empty array const keys: KeyValuePair[] = [];
Adding elements to the array
No matter if you use let or const, you can add elements using these common methods:
push(): Adds elements to the end of the array (this is what you're already using in your pipe):keys.push({ key: 'username', value: 'muthu' });- Spread operator: Creates a new array with the existing elements plus the new one (great if you want to avoid mutating the original array):
const updatedKeys = [...keys, { key: 'role', value: 'admin' }]; concat(): Merges the existing array with new elements, returning a new array:const updatedKeys = keys.concat({ key: 'email', value: 'muthu@example.com' });
Updated pipe code example
If you want to keep let and avoid TSLint errors, here's how your pipe would look:
@Pipe({name: 'keys'}) export class KeysPipe implements PipeTransform { transform(value, args:string[]) : any { // Disable prefer-const for this line // tslint:disable-next-line:prefer-const let keys = []; for (let key in value) { keys.push({key: key, value: value[key]}); } return keys; } }
Or, if you're open to using const (which is perfectly safe here), you can simplify it to:
@Pipe({name: 'keys'}) export class KeysPipe implements PipeTransform { transform(value, args:string[]) : any { const keys = []; // No TSLint error here for (let key in value) { keys.push({key: key, value: value[key]}); } return keys; } }
内容的提问来源于stack exchange,提问作者Muthukumar Jayamurugan

