MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / typeAssignable

Function typeAssignable

packages/language/src/utils.ts:85–103  ·  view source on GitHub ↗
(destType: ExpressionType, sourceType: ExpressionType, sourceExpr?: Expression)

Source from the content-addressed store, hash-verified

83 * Determines if the given sourceType is assignable to a destination of destType
84 */
85export function typeAssignable(destType: ExpressionType, sourceType: ExpressionType, sourceExpr?: Expression): boolean {
86 // implicit conversion from ISO datetime string to datetime
87 if (destType === 'DateTime' && sourceType === 'String' && sourceExpr && isStringLiteral(sourceExpr)) {
88 const literal = getStringLiteral(sourceExpr);
89 if (literal && isoDateTimeRegex.test(literal)) {
90 // implicitly convert to DateTime
91 sourceType = 'DateTime';
92 }
93 }
94
95 switch (destType) {
96 case 'Any':
97 return true;
98 case 'Float':
99 return sourceType === 'Any' || sourceType === 'Int' || sourceType === 'Float';
100 default:
101 return sourceType === 'Any' || sourceType === destType;
102 }
103}
104
105/**
106 * Maps a ZModel builtin type to expression type

Callers 3

validateInvocationArgMethod · 0.90
validateBinaryExprMethod · 0.90

Calls 2

isStringLiteralFunction · 0.90
getStringLiteralFunction · 0.85

Tested by

no test coverage detected