Skip to content

Commit

Permalink
fix: subtype check for literal and union types (part 2) (#1289)
Browse files Browse the repository at this point in the history
### Summary of Changes

The type checker previously did not regard `literal<1, "">` as a subtype
of `union<literal<1>, literal<"">`. This is fixed now.
  • Loading branch information
lars-reimann authored Dec 28, 2024
1 parent 911881c commit 56284cf
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,14 @@ export class SafeDsTypeChecker {
}

private constantIsSubtypeOf(constant: Constant, other: Type, options: TypeCheckOptions): boolean {
const classType = this.typeComputer().computeClassTypeForConstant(constant);
return this.isSubtypeOf(classType, other, options);
if (other instanceof ClassType) {
const classType = this.typeComputer().computeClassTypeForConstant(constant);
return this.isSubtypeOf(classType, other, options);
} else if (other instanceof LiteralType) {
return other.constants.some((otherConstant) => constant.equals(otherConstant));
} else {
return false;
}
}

private namedTupleTypeIsSubtypeOf(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,19 @@ const basic = async (): Promise<IsSubOrSupertypeOfTest[]> => {
type2: factory.createUnionType(coreTypes.Int, coreTypes.String),
expected: true,
},
{
type1: factory.createLiteralType(new IntConstant(1n), new StringConstant('')),
type2: factory.createUnionType(
factory.createLiteralType(new IntConstant(1n)),
factory.createLiteralType(new StringConstant('')),
),
expected: true,
},
{
type1: factory.createLiteralType(new IntConstant(1n)),
type2: factory.createUnionType(factory.createNamedTupleType()),
expected: false,
},
// Literal type to other
{
type1: factory.createLiteralType(), // Empty literal type
Expand Down

0 comments on commit 56284cf

Please sign in to comment.