Skip to content
This repository was archived by the owner on Mar 25, 2021. It is now read-only.
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/rules/noUnnecessaryTypeAssertionRule.ts
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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.]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"rules": {
"no-unnecessary-type-assertion": true
}
}