Skip to content

[no-object-literal-type-assertion] False positive when asserting function argument #66

Closed
@IlyaSemenov

Description

@IlyaSemenov

Repro

module.exports = {
    root: true,
    plugins: ["typescript"],
    parser: "typescript-eslint-parser",
    extends: ["plugin:typescript/recommended"],
}
interface Foo {
    foo?: number;
}

function print(x: Foo): void {
    console.log(x.foo)
}

print({ bar: 5 } as Foo)

Expected Result

No errors.

Actual Result

/Users/semenov/tmp/ts-rule/test.ts
  9:7  error  Type assertion on object literals is forbidden, use a type annotation instead  typescript/no-object-literal-type-assertion

✖ 1 problem (1 error, 0 warnings)

Additional Info

The primary purpose of the rule (as I see it) is to prevent object assignment such as const x = { ... } as T; which is better rewritten as const x: T = { ...};, but the rule disallows all other legitimate uses.

Versions

package version
eslint-plugin-typescript 1.0.0-rc.2
typescript-eslint-parser 21.0.2
typescript 3.2.2

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions