Skip to content

Commit

Permalink
More specific inference for constrained 'infer' types in template lit…
Browse files Browse the repository at this point in the history
…eral types
  • Loading branch information
rbuckton committed Mar 2, 2022
1 parent e64f04b commit ed2be49
Show file tree
Hide file tree
Showing 6 changed files with 1,116 additions and 6 deletions.
87 changes: 81 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21762,13 +21762,31 @@ namespace ts {
sourceEnd.slice(sourceEnd.length - endLen) !== targetEnd.slice(targetEnd.length - endLen);
}

function isValidBigIntString(s: string): boolean {
/**
* Tests whether the provided string can be parsed as a number.
* @param s The string to test.
* @param roundTripOnly Indicates the resulting number matches the input when converted back to a string.
*/
function isValidNumberString(s: string, roundTripOnly: boolean): boolean {
if (s === "") return false;
const n = +s;
return isFinite(n) && (!roundTripOnly || "" + n === s);
}

/**
* Tests whether the provided string can be parsed as a bigint.
* @param s The string to test.
* @param roundTripOnly Indicates the resulting bigint matches the input when converted back to a string.
*/
function isValidBigIntString(s: string, roundTripOnly: boolean): boolean {
if (s === "") return false;
const scanner = createScanner(ScriptTarget.ESNext, /*skipTrivia*/ false);
let success = true;
scanner.setOnError(() => success = false);
scanner.setText(s + "n");
let result = scanner.scan();
if (result === SyntaxKind.MinusToken) {
const negative = result === SyntaxKind.MinusToken;
if (negative) {
result = scanner.scan();
}
const flags = scanner.getTokenFlags();
Expand All @@ -21777,7 +21795,8 @@ namespace ts {
// * a bigint can be scanned, and that when it is scanned, it is
// * the full length of the input string (so the scanner is one character beyond the augmented input length)
// * it does not contain a numeric seperator (the `BigInt` constructor does not accept a numeric seperator in its input)
return success && result === SyntaxKind.BigIntLiteral && scanner.getTextPos() === (s.length + 1) && !(flags & TokenFlags.ContainsSeparator);
return success && result === SyntaxKind.BigIntLiteral && scanner.getTextPos() === (s.length + 1) && !(flags & TokenFlags.ContainsSeparator)
&& (!roundTripOnly || s === pseudoBigIntToString({ negative, base10Value: parsePseudoBigInt(scanner.getTokenValue()) }));
}

function isValidTypeForTemplateLiteralPlaceholder(source: Type, target: Type): boolean {
Expand All @@ -21786,8 +21805,8 @@ namespace ts {
}
if (source.flags & TypeFlags.StringLiteral) {
const value = (source as StringLiteralType).value;
return !!(target.flags & TypeFlags.Number && value !== "" && isFinite(+value) ||
target.flags & TypeFlags.BigInt && value !== "" && isValidBigIntString(value) ||
return !!(target.flags & TypeFlags.Number && isValidNumberString(value, /*roundTripOnly*/ false) ||
target.flags & TypeFlags.BigInt && isValidBigIntString(value, /*roundTripOnly*/ false) ||
target.flags & (TypeFlags.BooleanLiteral | TypeFlags.Nullable) && value === (target as IntrinsicType).intrinsicName);
}
if (source.flags & TypeFlags.TemplateLiteral) {
Expand Down Expand Up @@ -22365,7 +22384,63 @@ namespace ts {
// succeed. That would be a pointless and confusing outcome.
if (matches || every(target.texts, s => s.length === 0)) {
for (let i = 0; i < types.length; i++) {
inferFromTypes(matches ? matches[i] : neverType, types[i]);
const source = matches ? matches[i] : neverType;
const target = types[i];

// If we are inferring from a string literal type to a type variable whose constraint includes one of the
// allowed template literal placeholder types, infer from a literal type corresponding to the constraint.
let sourceTypes: Type[] | undefined;
if (source.flags & TypeFlags.StringLiteral && target.flags & TypeFlags.TypeVariable) {
const inferenceContext = getInferenceInfoForType(target);
const constraint = inferenceContext ? getConstraintOfTypeParameter(inferenceContext.typeParameter) : undefined;
if (inferenceContext && constraint) {
const str = (source as StringLiteralType).value;
const constraintTypes = constraint.flags & TypeFlags.Union ? (constraint as UnionType).types : [constraint];
for (const constraintType of constraintTypes) {
if (constraintType.flags & TypeFlags.StringLike) {
sourceTypes ??= [];
sourceTypes.push(source);
}
if (constraintType.flags & TypeFlags.NumberLike && isValidNumberString(str, /*roundTripOnly*/ true)) {
sourceTypes ??= [];
sourceTypes.push(getNumberLiteralType(+str));
}
if (constraintType.flags & TypeFlags.BigIntLike && isValidBigIntString(str, /*roundTripOnly*/ true)) {
const negative = str.startsWith("-");
const base10Value = parsePseudoBigInt(`${negative ? str.slice(1) : str}n`);
sourceTypes ??= [];
sourceTypes.push(getBigIntLiteralType({ negative, base10Value }));
}
if (constraintType.flags & TypeFlags.BooleanLike) {
if (str === trueType.intrinsicName) {
sourceTypes ??= [];
sourceTypes.push(trueType);
}
else if (str === falseType.intrinsicName) {
sourceTypes ??= [];
sourceTypes.push(falseType);
}
}
if (constraintType.flags & TypeFlags.Null && str === nullType.intrinsicName) {
sourceTypes ??= [];
sourceTypes.push(nullType);
}
if (constraintType.flags & TypeFlags.Undefined && str === undefinedType.intrinsicName) {
sourceTypes ??= [];
sourceTypes.push(undefinedType);
}
}
}
}

if (sourceTypes) {
for (const source of sourceTypes) {
inferFromTypes(source, target);
}
}
else {
inferFromTypes(source, target);
}
}
}
}
Expand Down
107 changes: 107 additions & 0 deletions tests/baselines/reference/templateLiteralTypes4.errors.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
tests/cases/conformance/types/literal/templateLiteralTypes4.ts(93,12): error TS2345: Argument of type '2' is not assignable to parameter of type '0 | 1'.
tests/cases/conformance/types/literal/templateLiteralTypes4.ts(97,12): error TS2345: Argument of type '2' is not assignable to parameter of type '0 | 1'.


==== tests/cases/conformance/types/literal/templateLiteralTypes4.ts (2 errors) ====
type Is<T extends U, U> = T;

type T0 = "100" extends `${Is<infer N, number>}` ? N : never; // 100
type T1 = "-100" extends `${Is<infer N, number>}` ? N : never; // -100
type T2 = "1.1" extends `${Is<infer N, number>}` ? N : never; // 1.1
type T3 = "8e-11" extends `${Is<infer N, number>}` ? N : never; // 8e-11 (0.00000000008)
type T4 = "0x10" extends `${Is<infer N, number>}` ? N : never; // number (not round-trippable)
type T5 = "0o10" extends `${Is<infer N, number>}` ? N : never; // number (not round-trippable)
type T6 = "0b10" extends `${Is<infer N, number>}` ? N : never; // number (not round-trippable)
type T7 = "10e2" extends `${Is<infer N, number>}` ? N : never; // number (not round-trippable)
type T8 = "abcd" extends `${Is<infer N, number>}` ? N : never; // never

type T10 = "100" extends `${Is<infer N, bigint>}` ? N : never; // 100n
type T11 = "-100" extends `${Is<infer N, bigint>}` ? N : never; // -100n
type T12 = "0x10" extends `${Is<infer N, bigint>}` ? N : never; // bigint (not round-trippable)
type T13 = "0o10" extends `${Is<infer N, bigint>}` ? N : never; // bigint (not round-trippable)
type T14 = "0b10" extends `${Is<infer N, bigint>}` ? N : never; // bigint (not round-trippable)
type T15 = "1.1" extends `${Is<infer N, bigint>}` ? N : never; // never
type T16 = "10e2" extends `${Is<infer N, bigint>}` ? N : never; // never
type T17 = "abcd" extends `${Is<infer N, bigint>}` ? N : never; // never

type T20 = "true" extends `${Is<infer T, boolean>}` ? T : never; // true
type T21 = "false" extends `${Is<infer T, boolean>}` ? T : never; // false
type T22 = "abcd" extends `${Is<infer T, boolean>}` ? T : never; // never

type T30 = "null" extends `${Is<infer T, null>}` ? T : never; // null
type T31 = "abcd" extends `${Is<infer T, null>}` ? T : never; // never

type T40 = "undefined" extends `${Is<infer T, undefined>}` ? T : never; // undefined
type T41 = "abcd" extends `${Is<infer T, undefined>}` ? T : never; // never

type T50 = "100" extends `${Is<infer T, string | number | bigint | boolean | null | undefined>}` ? T : never; // "100" | 100 | 100n
type T51 = "1.1" extends `${Is<infer T, string | number | bigint | boolean | null | undefined>}` ? T : never; // "100" | 1.1
type T52 = "true" extends `${Is<infer T, string | number | bigint | boolean | null | undefined>}` ? T : never; // "true" | true
type T53 = "false" extends `${Is<infer T, string | number | bigint | boolean | null | undefined>}` ? T : never; // "false" | false
type T54 = "null" extends `${Is<infer T, string | number | bigint | boolean | null | undefined>}` ? T : never; // "null" | null
type T55 = "undefined" extends `${Is<infer T, string | number | bigint | boolean | null | undefined>}` ? T : never; // "undefined" | undefined

type NumberFor<S extends string> = S extends `${Is<infer N, number>}` ? N : never;
type T60 = NumberFor<"100">; // 100
type T61 = NumberFor<any>; // never
type T62 = NumberFor<never>; // never

// example use case:
interface FieldDefinition {
readonly name: string;
readonly type: "i8" | "i16" | "i32" | "i64" | "u8" | "u16" | "u32" | "u64" | "f32" | "f64";
}

type FieldType<T extends FieldDefinition["type"]> =
T extends "i8" | "i16" | "i32" | "u8" | "u16" | "u32" | "f32" | "f64" ? number :
T extends "f32" | "f64" ? bigint :
never;

// Generates named members like `{ x: number, y: bigint }` from `[{ name: "x", type: "i32" }, { name: "y", type: "i64" }]`
type TypedObjectNamedMembers<TDef extends readonly FieldDefinition[]> = {
[P in TDef[number]["name"]]: FieldType<Extract<TDef[number], { readonly name: P }>["type"]>;
};

// Generates ordinal members like `{ 0: number, 1: bigint }` from `[{ name: "x", type: "i32" }, { name: "y", type: "i64" }]`
type TypedObjectOrdinalMembers<TDef extends readonly FieldDefinition[]> = {
[I in Extract<keyof TDef, `${number}`>]: FieldType<Extract<TDef[I], FieldDefinition>["type"]>;
};

// Default members
interface TypedObjectMembers<TDef extends readonly FieldDefinition[]> {
// get/set a field by name
get<K extends TDef[number]["name"]>(key: K): FieldType<Extract<TDef[number], { readonly name: K }>["type"]>;
set<K extends TDef[number]["name"]>(key: K, value: FieldType<Extract<TDef[number], { readonly name: K }>["type"]>): void;

// get/set a field by index
getIndex<I extends IndicesOf<TDef>>(index: I): FieldType<Extract<TDef[I], FieldDefinition>["type"]>;
setIndex<I extends IndicesOf<TDef>>(index: I, value: FieldType<Extract<TDef[I], FieldDefinition>["type"]>): void;
}

// Use constrained `infer` in template literal to get ordinal indices as numbers:
type IndicesOf<T> = NumberFor<Extract<keyof T, string>>; // ordinal indices as number literals

type TypedObject<TDef extends readonly FieldDefinition[]> =
& TypedObjectMembers<TDef>
& TypedObjectNamedMembers<TDef>
& TypedObjectOrdinalMembers<TDef>;

// NOTE: type would normally be created from something like `const Point = TypedObject([...])` from which we would infer the type
type Point = TypedObject<[
{ name: "x", type: "f64" },
{ name: "y", type: "f64" },
]>;

declare const p: Point;
p.getIndex(0); // ok, 0 is a valid index
p.getIndex(1); // ok, 1 is a valid index
p.getIndex(2); // error, 2 is not a valid index
~
!!! error TS2345: Argument of type '2' is not assignable to parameter of type '0 | 1'.

p.setIndex(0, 0); // ok, 0 is a valid index
p.setIndex(1, 0); // ok, 1 is a valid index
p.setIndex(2, 3); // error, 2 is not a valid index
~
!!! error TS2345: Argument of type '2' is not assignable to parameter of type '0 | 1'.

Loading

0 comments on commit ed2be49

Please sign in to comment.