public interface MyObject {
a: string;
b: string;
}
public m(): Partial<MyObject> {
return {
b: this.b,
a: this.a
};
}
with tslint.json configuration:
"object-literal-sort-keys": {
"options": "match-declaration-order",
"severity": "warn"
}
WARNING: object-literal-sort-keys The key 'a' is not sorted alphabetically
Use the match-declaration-order also for Partial
Other cases that it would be nice to have covered:
interface Foo { c: number; b: number; a: number; }
export const foo: Foo | undefined = { a: 10, b: 20, c: 30 };
The rule should enforce an order of { c, b, a }.
interface FooContainer { foo?: Foo }
interface Foo { c: number; b: number; a: number; }
export const foo: FooContainer = { foo: { a: 10, b: 20, c: 30 } };
The rule should enforce an order of { c, b, a }.
interface Foo { c: number; b: number; a: number; }
export const foo: Pick<Foo, 'c' | 'a'> = { a: 10, c: 30 };
The rule should enforce an order of { c, a }. I believe it would make the most sense to get this order from the 'c' | 'a' type, since that is the type the compiler iterates over to build the resulting type. Or, more generally:
type HomebrewReadonly<T> = { readonly [K in keyof T]: T[K] };
interface Foo { c: number; b: number; a: number; }
export const foo: HomebrewReadonly<Foo> = { a: 10, b: 20, c: 30 };
The rule should enforce an order of { c, b, a }. The order should come from whatever is on the RHS of the in operator. In the case of [K in keyof T], that would be keyof T, which gets the keys from T, and T in this case is Foo.
โ ๏ธ TSLint's time has come! โ ๏ธ
TSLint is no longer accepting most feature requests per #4534. See typescript-eslint.io for the new, shiny way to lint your TypeScript code with ESLint. โจ
It was a pleasure open sourcing with you all!
๐ค Beep boop! ๐ TSLint is deprecated ๐ _(#4534)_ and you should switch to typescript-eslint! ๐ค
๐ This issue is being locked to prevent further unnecessary discussions. Thank you! ๐
Most helpful comment
Other cases that it would be nice to have covered:
Union types
The rule should enforce an order of
{ c, b, a }.Values contained in an optional field
The rule should enforce an order of
{ c, b, a }.Pick
The rule should enforce an order of
{ c, a }. I believe it would make the most sense to get this order from the'c' | 'a'type, since that is the type the compiler iterates over to build the resulting type. Or, more generally:Any mapped type
The rule should enforce an order of
{ c, b, a }. The order should come from whatever is on the RHS of theinoperator. In the case of[K in keyof T], that would bekeyof T, which gets the keys fromT, andTin this case isFoo.