From 96ad01d46cd8920d08479e357d9e8da56e6e98da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=98=A4=EB=B3=91=EC=A7=84?= <64676070+sunrabbit123@users.noreply.github.com> Date: Sun, 20 Aug 2023 12:16:17 +0900 Subject: [PATCH 1/2] feat: Support accessing nom primitive constraint of an empty object (#1072) **Description:** ```ts function l(s: string, tp: T[P]): void { tp = s; } ``` --- crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs | 3 +++ .../nonPrimitiveConstraintOfIndexAccessType.error-diff.json | 3 +-- .../nonPrimitiveConstraintOfIndexAccessType.stats.rust-debug | 4 ++-- crates/stc_ts_type_checker/tests/tsc-stats.rust-debug | 4 ++-- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs b/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs index 84c11c35de..0e81cc3ecc 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/expr/mod.rs @@ -2513,6 +2513,9 @@ impl Analyzer<'_, '_> { } if type_mode == TypeOfMode::LValue { + if prop.ty().is_type_param() && members.is_empty() { + return Ok(Type::never(span, Default::default())); + } return Ok(Type::any(span, Default::default())); } diff --git a/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.error-diff.json b/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.error-diff.json index f8c3529e5f..b6be05c17d 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.error-diff.json +++ b/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.error-diff.json @@ -1,10 +1,9 @@ { "required_errors": { - "TS2322": 2 + "TS2322": 1 }, "required_error_lines": { "TS2322": [ - 25, 28 ] }, diff --git a/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.stats.rust-debug b/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.stats.rust-debug index 13ac66ffd7..4074730ba8 100644 --- a/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.stats.rust-debug @@ -1,6 +1,6 @@ Stats { - required_error: 2, - matched_error: 8, + required_error: 1, + matched_error: 9, extra_error: 0, panic: 0, } \ No newline at end of file diff --git a/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug b/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug index 02553a0cb6..57039bc151 100644 --- a/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug +++ b/crates/stc_ts_type_checker/tests/tsc-stats.rust-debug @@ -1,6 +1,6 @@ Stats { - required_error: 3502, - matched_error: 6533, + required_error: 3501, + matched_error: 6534, extra_error: 764, panic: 73, } \ No newline at end of file From 277b967403b38526a373f5aed05c0320f744feb4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EC=98=A4=EB=B3=91=EC=A7=84?= <64676070+sunrabbit123@users.noreply.github.com> Date: Sun, 20 Aug 2023 12:27:29 +0900 Subject: [PATCH 2/2] fix: Fix panic from `parse().unwrap()` at generic inference (#1070) **Description:** ```rs if r.is_num() { match src.parse() { Ok(v) => { return Type::Lit(LitType { span, lit: RTsLit::Number(RNumber { span, value: v, raw: None }), metadata: Default::default(), tracker: Default::default(), }) } Err(..) => { return Type::Keyword(KeywordType { span, kind: TsKeywordTypeKind::TsNumberKeyword, metadata: Default::default(), tracker: Default::default(), }) } } } ``` --- .../src/analyzer/generic/inference.rs | 29 ++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs b/crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs index 3aca4dcd45..f09cc6a88d 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs @@ -533,6 +533,7 @@ impl Analyzer<'_, '_> { // variable whose constraint includes one of the // allowed template literal placeholder types, infer from a // literal type corresponding to the constraint. + // ref: https://github.com/microsoft/TypeScript/blob/b5557271a51704e0469dd86974335a4775a68ffd/src/compiler/checker.ts#L25342 if source.is_str_lit() && (target.is_type_param() || target.is_infer()) { if let Type::Infer(InferType { type_param: @@ -624,16 +625,24 @@ impl Analyzer<'_, '_> { } if r.is_num() { - return Type::Lit(LitType { - span, - lit: RTsLit::Number(RNumber { - span, - value: src.parse().unwrap(), - raw: None, - }), - metadata: Default::default(), - tracker: Default::default(), - }); + match src.parse() { + Ok(v) => { + return Type::Lit(LitType { + span, + lit: RTsLit::Number(RNumber { span, value: v, raw: None }), + metadata: Default::default(), + tracker: Default::default(), + }) + } + Err(..) => { + return Type::Keyword(KeywordType { + span, + kind: TsKeywordTypeKind::TsNumberKeyword, + metadata: Default::default(), + tracker: Default::default(), + }) + } + } } if l.is_enum_type() || l.is_enum_variant() {