Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: subtype check for literal and union types #1288

Merged
merged 1 commit into from
Dec 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export class SafeDsTypeChecker {
// Handle union types
if (type instanceof UnionType) {
return type.types.every((it) => this.isSubtypeOf(it, other, options));
} else if (other instanceof UnionType) {
} else if (other instanceof UnionType && !(type instanceof LiteralType)) {
// For literal types the quantifiers would be the wrong way around
return other.types.some((it) => this.isSubtypeOf(type, it, options));
}

Expand Down Expand Up @@ -264,17 +265,21 @@ export class SafeDsTypeChecker {
return true;
}

return type.constants.every((constant) => this.constantIsSubtypeOfClassType(constant, other, options));
return type.constants.every((constant) => this.constantIsSubtypeOf(constant, other, options));
} else if (other instanceof LiteralType) {
return type.constants.every((constant) =>
other.constants.some((otherConstant) => constant.equals(otherConstant)),
);
} else if (other instanceof UnionType) {
return type.constants.every((constant) =>
other.types.some((it) => this.constantIsSubtypeOf(constant, it, options)),
);
} else {
return false;
}
}

private constantIsSubtypeOfClassType(constant: Constant, other: ClassType, options: TypeCheckOptions): boolean {
private constantIsSubtypeOf(constant: Constant, other: Type, options: TypeCheckOptions): boolean {
const classType = this.typeComputer().computeClassTypeForConstant(constant);
return this.isSubtypeOf(classType, other, options);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,11 @@ const basic = async (): Promise<IsSubOrSupertypeOfTest[]> => {
type2: factory.createUnionType(coreTypes.String),
expected: false,
},
{
type1: factory.createLiteralType(new IntConstant(1n), new StringConstant('')),
type2: factory.createUnionType(coreTypes.Int, coreTypes.String),
expected: true,
},
// Literal type to other
{
type1: factory.createLiteralType(), // Empty literal type
Expand Down
Loading