Skip to content

Commit

Permalink
fix assertMatches to use Type.matches, enables correct handling of sp…
Browse files Browse the repository at this point in the history
…ecial types like <text(42)>
  • Loading branch information
benStre committed Jan 17, 2024
1 parent 02197e4 commit 6b0fe0a
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions types/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -695,12 +695,12 @@ export class Type<T = any> extends ExtensibleFunction {
}

public static assertMatches<T extends Type>(value:RefOrValue<any>, type:type_clause): asserts value is (T extends Type<infer TT> ? TT : any) {
const res = Type.matchesType(Type.ofValue(value), type, value, true);
const res = Type.matches(value, type, true);
if (!res) throw new ValueError("Value must be of type " + type)
}

// check if root type of value matches exactly
public static matches<T extends Type>(value:RefOrValue<any>, type:type_clause): value is (T extends Type<infer TT> ? TT : any) {
public static matches<T extends Type>(value:RefOrValue<any>, type:type_clause, throwInvalidAssertion = false): value is (T extends Type<infer TT> ? TT : any) {
value = Ref.collapseValue(value, true, true);
// value has a matching DX_TEMPLATE
if (type instanceof Type && type.template && value[DX_TEMPLATE] && this.matchesTemplate(value[DX_TEMPLATE], type.template)) return true;
Expand All @@ -712,7 +712,7 @@ export class Type<T = any> extends ExtensibleFunction {
return value.length <= type.parameters[0];
}

return Type.matchesType(Type.ofValue(value), type, value);
return Type.matchesType(Type.ofValue(value), type, value, throwInvalidAssertion);
}

public static extends(type:Type, extends_type:type_clause){
Expand Down

0 comments on commit 6b0fe0a

Please sign in to comment.