@@ -21773,6 +21773,15 @@ namespace ts {
21773
21773
return isFinite(n) && (!roundTripOnly || "" + n === s);
21774
21774
}
21775
21775
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
+
21776
21785
/**
21777
21786
* Tests whether the provided string can be parsed as a bigint.
21778
21787
* @param s The string to test.
@@ -22393,41 +22402,22 @@ namespace ts {
22393
22402
if (source.flags & TypeFlags.StringLiteral && target.flags & TypeFlags.TypeVariable) {
22394
22403
const inferenceContext = getInferenceInfoForType(target);
22395
22404
const constraint = inferenceContext ? getConstraintOfTypeParameter(inferenceContext.typeParameter) : undefined;
22396
- if (inferenceContext && constraint) {
22405
+ if (constraint) {
22397
22406
const str = (source as StringLiteralType).value;
22398
22407
const constraintTypes = constraint.flags & TypeFlags.Union ? (constraint as UnionType).types : [constraint];
22399
22408
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) {
22429
22419
sourceTypes ??= [];
22430
- sourceTypes.push(undefinedType );
22420
+ sourceTypes.push(sourceType );
22431
22421
}
22432
22422
}
22433
22423
}
0 commit comments