From 93399ff260dba519be87ef9cca234cb76fe50986 Mon Sep 17 00:00:00 2001 From: York Yao Date: Mon, 29 Jun 2020 07:20:02 +0800 Subject: [PATCH] fix: check contextual type too to avoid false positive #56 --- packages/core/src/checker.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/core/src/checker.ts b/packages/core/src/checker.ts index 601c473..a00dabf 100644 --- a/packages/core/src/checker.ts +++ b/packages/core/src/checker.ts @@ -28,10 +28,19 @@ function collectNotAny(node: ts.Node, { file, sourceFile, typeCheckResult, debug } function collectData(node: ts.Node, context: FileContext) { + const types: ts.Type[] = [] const type = context.checker.getTypeAtLocation(node) if (type) { + types.push(type) + } + const contextualType = context.checker.getContextualType(node as ts.Expression) + if (contextualType) { + types.push(contextualType) + } + + if (types.length > 0) { context.typeCheckResult.totalCount++ - if (typeIsStrictAny(type, context.strict)) { + if (types.every((t) => typeIsStrictAny(t, context.strict))) { const success = collectAny(node, context) if (!success) { collectNotAny(node, context, type)