Add 'object-literal-contextual-type' rule#2581
Add 'object-literal-contextual-type' rule#2581andy-hanson wants to merge 4 commits intopalantir:masterfrom
Conversation
6ad5b97 to
b1707fb
Compare
|
Rebased. |
b1707fb to
cdd9eef
Compare
giladgray
left a comment
There was a problem hiding this comment.
typescript has gotten better at handling objects like this. this rule is too aggressive (requiring too many changes and disables in existing source code).
| }; | ||
|
|
||
| public static ARGUMENT_DESCRIPTOR_BLOCK = { | ||
| public static ARGUMENT_DESCRIPTOR_BLOCK: {} = { |
There was a problem hiding this comment.
this isn't even correct. it has a much more advanced type than the empty object.
There was a problem hiding this comment.
{} is a correct type. Anything but null or undefined or void is assignable to {}.
| * limitations under the License. | ||
| */ | ||
|
|
||
| // tslint:disable object-literal-contextual-type |
There was a problem hiding this comment.
This is a comment, how does it break the API?
| interface I { x: number; } | ||
| function f(): I { | ||
| const res = { x: 0, y: 0 }; | ||
| return res; |
There was a problem hiding this comment.
this error can be avoided so many ways that don't require a rather aggressive lint rule...
There was a problem hiding this comment.
How would it be avoided in general? I don't believe typescript's behavior with respect to object literals has changed much in the past year.
Often people will think they're getting a contextual type when they really aren't, e.g. a result from a callback:
interface Options { foo?: number; bar?: number }
const arr: Options[] = [1, 2, 3].map(n => ({ foo: n, baa: n }));The error is fixed by changing to (n): Options, but I wouldn't want a lint rule to require all arrow functions to have explicit return types.
If there are better ways of avoiding this error I'd like to hear about them.
PR checklist
Overview of change:
Added the
object-literal-contextual-typerule, which warns for an object literal with no contextual type (which may have unchecked excess properties).This branch is based on top of #2544 and will be rebased once that's in.
CHANGELOG.md entry:
[new-rule]
object-literal-contextual-type