tslint-immutable icon indicating copy to clipboard operation
tslint-immutable copied to clipboard

Prefer type literal over interface

Open goodmind opened this issue 8 years ago • 5 comments

This will be cool addition to https://github.com/jonaskello/tslint-immutable#no-mixed-interface or as standalone rule I think. Even better to allow functions only in interfaces and data only in type literals

goodmind avatar Mar 16 '17 12:03 goodmind

Thanks for the suggestion!

So if I understand correctly you want to disallow this:

interface Foo {
  readonly bar: number
}

and only allow:

type Foo = {
  readonly bar: number
}

Could you elaborate about the benefits of this?

My experience is that type aliases has some drawbacks for error reporting. When you use interface the errors usually include the interface name but for type aliases it prints the whole structure.

jonaskello avatar Mar 28 '17 20:03 jonaskello

Oh, I think that your point is correct. But is no-mixed-interface works with type literals?

goodmind avatar Mar 29 '17 07:03 goodmind

I think maybe we need an additional rule called no-mixed-types that mirrors what no-mixed-interfacedoes but for type declarations.

jonaskello avatar Apr 01 '17 12:04 jonaskello

Actually I think there are two cases that are not covered:

  • Explicitly declaring mixed types with type.
  • Implicitly declaring mixed types via type literals.

jonaskello avatar Apr 01 '17 17:04 jonaskello

tslint-microsoft-contrib has the opposite of this: interface-over-type-literal

ghost avatar Nov 29 '17 15:11 ghost