Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

no-unnecessary-type-assertion: Don't check ! if --strictNullChecks is not enabled #3724

Merged
merged 4 commits into from Jun 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/rules/noUnnecessaryTypeAssertionRule.ts
Expand Up @@ -45,7 +45,13 @@ export class Rule extends Lint.Rules.TypedRule {

public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithWalker(
new Walker(sourceFile, this.ruleName, this.ruleArguments, program.getTypeChecker()),
new Walker(
sourceFile,
this.ruleName,
this.ruleArguments,
program.getTypeChecker(),
!!program.getCompilerOptions().strictNullChecks,
),
);
}
}
Expand All @@ -56,6 +62,7 @@ class Walker extends Lint.AbstractWalker<string[]> {
ruleName: string,
options: string[],
private readonly checker: ts.TypeChecker,
private readonly strictNullChecks: boolean,
) {
super(sourceFile, ruleName, options);
}
Expand All @@ -64,7 +71,9 @@ class Walker extends Lint.AbstractWalker<string[]> {
const cb = (node: ts.Node): void => {
switch (node.kind) {
case ts.SyntaxKind.NonNullExpression:
this.checkNonNullAssertion(node as ts.NonNullExpression);
if (this.strictNullChecks) {
this.checkNonNullAssertion(node as ts.NonNullExpression);
}
break;
case ts.SyntaxKind.TypeAssertionExpression:
case ts.SyntaxKind.AsExpression:
Expand Down
@@ -0,0 +1,6 @@
declare const x: string | undefined;
x!;

declare const y: string;
y as string;
~~~~~~~~~~~ [This assertion is unnecessary since it does not change the type of the expression.]
@@ -0,0 +1 @@
{}
@@ -0,0 +1,5 @@
{
"rules": {
"no-unnecessary-type-assertion": true
}
}