Skip to content

Commit c8da16a

Browse files
committed
PR feedback
1 parent 0758d84 commit c8da16a

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

src/compiler/checker.ts

Lines changed: 21 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22120,6 +22120,15 @@ namespace ts {
2212022120
return isFinite(n) && (!roundTripOnly || "" + n === s);
2212122121
}
2212222122

22123+
/**
22124+
* @param text a valid bigint string excluding a trailing `n`, but including a possible prefix `-`. Use `isValidBigIntString(text, roundTripOnly)` before calling this function.
22125+
*/
22126+
function parseBigIntLiteralType(text: string) {
22127+
const negative = text.startsWith("-");
22128+
const base10Value = parsePseudoBigInt(`${negative ? text.slice(1) : text}n`);
22129+
return getBigIntLiteralType({ negative, base10Value });
22130+
}
22131+
2212322132
/**
2212422133
* Tests whether the provided string can be parsed as a bigint.
2212522134
* @param s The string to test.
@@ -22740,41 +22749,22 @@ namespace ts {
2274022749
if (source.flags & TypeFlags.StringLiteral && target.flags & TypeFlags.TypeVariable) {
2274122750
const inferenceContext = getInferenceInfoForType(target);
2274222751
const constraint = inferenceContext ? getConstraintOfTypeParameter(inferenceContext.typeParameter) : undefined;
22743-
if (inferenceContext && constraint) {
22752+
if (constraint) {
2274422753
const str = (source as StringLiteralType).value;
2274522754
const constraintTypes = constraint.flags & TypeFlags.Union ? (constraint as UnionType).types : [constraint];
2274622755
for (const constraintType of constraintTypes) {
22747-
if (constraintType.flags & TypeFlags.StringLike) {
22748-
sourceTypes ??= [];
22749-
sourceTypes.push(source);
22750-
}
22751-
if (constraintType.flags & TypeFlags.NumberLike && isValidNumberString(str, /*roundTripOnly*/ true)) {
22752-
sourceTypes ??= [];
22753-
sourceTypes.push(getNumberLiteralType(+str));
22754-
}
22755-
if (constraintType.flags & TypeFlags.BigIntLike && isValidBigIntString(str, /*roundTripOnly*/ true)) {
22756-
const negative = str.startsWith("-");
22757-
const base10Value = parsePseudoBigInt(`${negative ? str.slice(1) : str}n`);
22758-
sourceTypes ??= [];
22759-
sourceTypes.push(getBigIntLiteralType({ negative, base10Value }));
22760-
}
22761-
if (constraintType.flags & TypeFlags.BooleanLike) {
22762-
if (str === trueType.intrinsicName) {
22763-
sourceTypes ??= [];
22764-
sourceTypes.push(trueType);
22765-
}
22766-
else if (str === falseType.intrinsicName) {
22767-
sourceTypes ??= [];
22768-
sourceTypes.push(falseType);
22769-
}
22770-
}
22771-
if (constraintType.flags & TypeFlags.Null && str === nullType.intrinsicName) {
22772-
sourceTypes ??= [];
22773-
sourceTypes.push(nullType);
22774-
}
22775-
if (constraintType.flags & TypeFlags.Undefined && str === undefinedType.intrinsicName) {
22756+
const sourceType =
22757+
constraintType.flags & TypeFlags.StringLike ? source :
22758+
constraintType.flags & TypeFlags.NumberLike && isValidNumberString(str, /*roundTripOnly*/ true) ? getNumberLiteralType(+str) :
22759+
constraintType.flags & TypeFlags.BigIntLike && isValidBigIntString(str, /*roundTripOnly*/ true) ? parseBigIntLiteralType(str) :
22760+
constraintType.flags & TypeFlags.BooleanLike && str === trueType.intrinsicName ? trueType :
22761+
constraintType.flags & TypeFlags.BooleanLike && str === falseType.intrinsicName ? falseType :
22762+
constraintType.flags & TypeFlags.Null && str === nullType.intrinsicName ? nullType :
22763+
constraintType.flags & TypeFlags.Undefined && str === undefinedType.intrinsicName ? undefinedType :
22764+
undefined;
22765+
if (sourceType) {
2277622766
sourceTypes ??= [];
22777-
sourceTypes.push(undefinedType);
22767+
sourceTypes.push(sourceType);
2277822768
}
2277922769
}
2278022770
}

0 commit comments

Comments
 (0)