Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fix panic from parse().unwrap() at generic inference #1070

Merged
merged 3 commits into from
Aug 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 19 additions & 10 deletions crates/stc_ts_file_analyzer/src/analyzer/generic/inference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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() {
Expand Down
Loading