Skip to content

Commit

Permalink
fix: exclude foo as unknown in strict mode
Browse files Browse the repository at this point in the history
54
  • Loading branch information
plantain-00 committed Jun 21, 2020
1 parent 2ac44ac commit b6aa193
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ name | type | description

If the identifiers' type arguments exist and contain at least one `any`, like `any[]`, `ReadonlyArray<any>`, `Promise<any>`, `Foo<number, any>`, it will be considered as `any` too

Type assertion, like `foo as string`, `foo!`, `<string>foo` will be considered as uncovered, exclude `foo as const`, `<const>foo`
Type assertion, like `foo as string`, `foo!`, `<string>foo` will be considered as uncovered, exclude `foo as const`, `<const>foo`, `foo as unknown`

Also, future minor release may introduce stricter type check in this mode, which may lower the type coverage rate

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
],
"private": true,
"typeCoverage": {
"atLeast": 96.37
"atLeast": 96.44
}
}
14 changes: 9 additions & 5 deletions packages/core/src/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,15 @@ function checkNodes(nodes: ts.NodeArray<ts.Node> | undefined, context: FileConte

function checkTypeAssertion(node: ts.Node, context: FileContext) {
if (context.strict) {
// exclude `foo as const` and `<const>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 `<const>foo`
if (ts.isTypeReferenceNode(node.type) && node.type.getText() === 'const') {
return
}
// exclude `foo as unknown` and `<unknown>foo`
if (node.type.kind === ts.SyntaxKind.UnknownKeyword) {
return
}
}
const success = collectAny(node, context)
if (success) {
Expand Down

0 comments on commit b6aa193

Please sign in to comment.