From b6aa193e4b73fb6c8ade0d80116589f51f325369 Mon Sep 17 00:00:00 2001 From: York Yao Date: Sun, 21 Jun 2020 16:31:32 +0800 Subject: [PATCH] fix: exclude foo as unknown in strict mode 54 --- README.md | 2 +- package.json | 2 +- packages/core/src/checker.ts | 14 +++++++++----- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 3a0ecee..b98ec6c 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ name | type | description If the identifiers' type arguments exist and contain at least one `any`, like `any[]`, `ReadonlyArray`, `Promise`, `Foo`, it will be considered as `any` too -Type assertion, like `foo as string`, `foo!`, `foo` will be considered as uncovered, exclude `foo as const`, `foo` +Type assertion, like `foo as string`, `foo!`, `foo` will be considered as uncovered, exclude `foo as const`, `foo`, `foo as unknown` Also, future minor release may introduce stricter type check in this mode, which may lower the type coverage rate diff --git a/package.json b/package.json index f387c26..8d7c2e7 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,6 @@ ], "private": true, "typeCoverage": { - "atLeast": 96.37 + "atLeast": 96.44 } } diff --git a/packages/core/src/checker.ts b/packages/core/src/checker.ts index 9813b09..d45d884 100644 --- a/packages/core/src/checker.ts +++ b/packages/core/src/checker.ts @@ -67,11 +67,15 @@ function checkNodes(nodes: ts.NodeArray | undefined, context: FileConte function checkTypeAssertion(node: ts.Node, context: FileContext) { if (context.strict) { - // exclude `foo as const` and `foo` - if ((ts.isAsExpression(node) || ts.isTypeAssertion(node)) - && ts.isTypeReferenceNode(node.type) - && node.type.getText() === 'const') { - return + if ((ts.isAsExpression(node) || ts.isTypeAssertion(node))) { + // exclude `foo as const` and `foo` + if (ts.isTypeReferenceNode(node.type) && node.type.getText() === 'const') { + return + } + // exclude `foo as unknown` and `foo` + if (node.type.kind === ts.SyntaxKind.UnknownKeyword) { + return + } } const success = collectAny(node, context) if (success) {