Skip to content

Commit 1bc376f

Browse files
committed
PR feedback
1 parent ed2be49 commit 1bc376f

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
@@ -21773,6 +21773,15 @@ namespace ts {
2177321773
return isFinite(n) && (!roundTripOnly || "" + n === s);
2177421774
}
2177521775

21776+
/**
21777+
* @param text a valid bigint string excluding a trailing `n`, but including a possible prefix `-`. Use `isValidBigIntString(text, roundTripOnly)` before calling this function.
21778+
*/
21779+
function parseBigIntLiteralType(text: string) {
21780+
const negative = text.startsWith("-");
21781+
const base10Value = parsePseudoBigInt(`${negative ? text.slice(1) : text}n`);
21782+
return getBigIntLiteralType({ negative, base10Value });
21783+
}
21784+
2177621785
/**
2177721786
* Tests whether the provided string can be parsed as a bigint.
2177821787
* @param s The string to test.
@@ -22393,41 +22402,22 @@ namespace ts {
2239322402
if (source.flags & TypeFlags.StringLiteral && target.flags & TypeFlags.TypeVariable) {
2239422403
const inferenceContext = getInferenceInfoForType(target);
2239522404
const constraint = inferenceContext ? getConstraintOfTypeParameter(inferenceContext.typeParameter) : undefined;
22396-
if (inferenceContext && constraint) {
22405+
if (constraint) {
2239722406
const str = (source as StringLiteralType).value;
2239822407
const constraintTypes = constraint.flags & TypeFlags.Union ? (constraint as UnionType).types : [constraint];
2239922408
for (const constraintType of constraintTypes) {
22400-
if (constraintType.flags & TypeFlags.StringLike) {
22401-
sourceTypes ??= [];
22402-
sourceTypes.push(source);
22403-
}
22404-
if (constraintType.flags & TypeFlags.NumberLike && isValidNumberString(str, /*roundTripOnly*/ true)) {
22405-
sourceTypes ??= [];
22406-
sourceTypes.push(getNumberLiteralType(+str));
22407-
}
22408-
if (constraintType.flags & TypeFlags.BigIntLike && isValidBigIntString(str, /*roundTripOnly*/ true)) {
22409-
const negative = str.startsWith("-");
22410-
const base10Value = parsePseudoBigInt(`${negative ? str.slice(1) : str}n`);
22411-
sourceTypes ??= [];
22412-
sourceTypes.push(getBigIntLiteralType({ negative, base10Value }));
22413-
}
22414-
if (constraintType.flags & TypeFlags.BooleanLike) {
22415-
if (str === trueType.intrinsicName) {
22416-
sourceTypes ??= [];
22417-
sourceTypes.push(trueType);
22418-
}
22419-
else if (str === falseType.intrinsicName) {
22420-
sourceTypes ??= [];
22421-
sourceTypes.push(falseType);
22422-
}
22423-
}
22424-
if (constraintType.flags & TypeFlags.Null && str === nullType.intrinsicName) {
22425-
sourceTypes ??= [];
22426-
sourceTypes.push(nullType);
22427-
}
22428-
if (constraintType.flags & TypeFlags.Undefined && str === undefinedType.intrinsicName) {
22409+
const sourceType =
22410+
constraintType.flags & TypeFlags.StringLike ? source :
22411+
constraintType.flags & TypeFlags.NumberLike && isValidNumberString(str, /*roundTripOnly*/ true) ? getNumberLiteralType(+str) :
22412+
constraintType.flags & TypeFlags.BigIntLike && isValidBigIntString(str, /*roundTripOnly*/ true) ? parseBigIntLiteralType(str) :
22413+
constraintType.flags & TypeFlags.BooleanLike && str === trueType.intrinsicName ? trueType :
22414+
constraintType.flags & TypeFlags.BooleanLike && str === falseType.intrinsicName ? falseType :
22415+
constraintType.flags & TypeFlags.Null && str === nullType.intrinsicName ? nullType :
22416+
constraintType.flags & TypeFlags.Undefined && str === undefinedType.intrinsicName ? undefinedType :
22417+
undefined;
22418+
if (sourceType) {
2242922419
sourceTypes ??= [];
22430-
sourceTypes.push(undefinedType);
22420+
sourceTypes.push(sourceType);
2243122421
}
2243222422
}
2243322423
}

0 commit comments

Comments
 (0)