Tslint: [Feature Request] object-literal-sort-keys - match-declaration-order also for Partial<T>

Created on 8 Feb 2018  ยท  3Comments  ยท  Source: palantir/tslint

Feature Request

  • __TSLint version__: 5.9.1
  • __TypeScript version__: 2.6.2
  • __Running TSLint via__: CLI / VSCode

TypeScript code being linted

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"
}

Actual behavior

WARNING: object-literal-sort-keys The key 'a' is not sorted alphabetically

Expected behavior

Use the match-declaration-order also for Partial

Hard Requires Type Checker Rule Enhancement ๐ŸŒน R.I.P. ๐ŸŒน

Most helpful comment

Other cases that it would be nice to have covered:

Union types

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 }.

Values contained in an optional field

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 }.

Pick

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:

Any mapped type

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.

All 3 comments

Other cases that it would be nice to have covered:

Union types

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 }.

Values contained in an optional field

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 }.

Pick

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:

Any mapped type

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! ๐Ÿ‘‹

Was this page helpful?
0 / 5 - 0 ratings

Related issues

zewa666 picture zewa666  ยท  3Comments

ypresto picture ypresto  ยท  3Comments

rajinder-yadav picture rajinder-yadav  ยท  3Comments

denkomanceski picture denkomanceski  ยท  3Comments

jacob-robertson picture jacob-robertson  ยท  3Comments