From 4d64d1c779e9365b2fddd345d96688726f6e40b2 Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Sun, 20 Aug 2023 00:53:11 +0900 Subject: [PATCH 1/2] Fix: panic from parse().unwrap() --- .../src/analyzer/generic/inference.rs | 28 ++++++++++++------- 1 file changed, 18 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..75f5edbf88 100644 --- a/crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs +++ b/crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs @@ -624,16 +624,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() { From 3697d8dd2e4434bb9ad7696ba6a50f60b28e0e07 Mon Sep 17 00:00:00 2001 From: sunrabbit123 Date: Sun, 20 Aug 2023 01:12:29 +0900 Subject: [PATCH 2/2] docs : add comment --- crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs | 1 + 1 file changed, 1 insertion(+) 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 75f5edbf88..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: