@@ -22120,6 +22120,15 @@ namespace ts {
22120
22120
return isFinite(n) && (!roundTripOnly || "" + n === s);
22121
22121
}
22122
22122
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
+
22123
22132
/**
22124
22133
* Tests whether the provided string can be parsed as a bigint.
22125
22134
* @param s The string to test.
@@ -22740,41 +22749,22 @@ namespace ts {
22740
22749
if (source.flags & TypeFlags.StringLiteral && target.flags & TypeFlags.TypeVariable) {
22741
22750
const inferenceContext = getInferenceInfoForType(target);
22742
22751
const constraint = inferenceContext ? getConstraintOfTypeParameter(inferenceContext.typeParameter) : undefined;
22743
- if (inferenceContext && constraint) {
22752
+ if (constraint) {
22744
22753
const str = (source as StringLiteralType).value;
22745
22754
const constraintTypes = constraint.flags & TypeFlags.Union ? (constraint as UnionType).types : [constraint];
22746
22755
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) {
22776
22766
sourceTypes ??= [];
22777
- sourceTypes.push(undefinedType );
22767
+ sourceTypes.push(sourceType );
22778
22768
}
22779
22769
}
22780
22770
}
0 commit comments