Skip to content

Commit

Permalink
fix(eslint-plugin): [nuta] correctly handle null/undefined separation (
Browse files Browse the repository at this point in the history
  • Loading branch information
bradzacher committed Nov 16, 2019
1 parent d1de3a7 commit 9829dd3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
15 changes: 11 additions & 4 deletions packages/eslint-plugin/src/rules/no-unnecessary-type-assertion.ts
Expand Up @@ -216,10 +216,17 @@ export default util.createRule<Options, MessageIds>({
contextualType,
ts.TypeFlags.Null,
);
if (
(typeIncludesUndefined && contextualTypeIncludesUndefined) ||
(typeIncludesNull && contextualTypeIncludesNull)
) {

// make sure that the parent accepts the same types
// i.e. assigning `string | null | undefined` to `string | undefined` is invalid
const isValidUndefined = typeIncludesUndefined
? contextualTypeIncludesUndefined
: true;
const isValidNull = typeIncludesNull
? contextualTypeIncludesNull
: true;

if (isValidUndefined && isValidNull) {
context.report({
node,
messageId: 'contextuallyUnnecessary',
Expand Down
Expand Up @@ -104,6 +104,17 @@ class Mx {
private prop = 1;
}
`,
// https://github.com/typescript-eslint/typescript-eslint/issues/1199
`
function testFunction(_param: string | undefined): void { /* noop */ }
const value = 'test' as string | null | undefined
testFunction(value!)
`,
`
function testFunction(_param: string | null): void { /* noop */ }
const value = 'test' as string | null | undefined
testFunction(value!)
`,
],

invalid: [
Expand Down

0 comments on commit 9829dd3

Please sign in to comment.