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: Model with spread indexer shouldn't validate explicit properites #3163

Merged
merged 4 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
86 changes: 84 additions & 2 deletions packages/compiler/src/core/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5556,8 +5556,12 @@
}),
],
];
} else if (target.kind === "Model" && target.indexer !== undefined && source.kind === "Model") {
return isIndexerValid(
} else if (
target.kind === "Model" &&
isArrayModelType(program, target) &&
source.kind === "Model"
) {
return areIndexAreCompatible(
source,
target as Model & { indexer: ModelIndexer },
diagnosticTarget,
Expand Down Expand Up @@ -5718,6 +5722,8 @@
): [Related, Diagnostic[]] {
relationCache.set([source, target], Related.maybe);
const diagnostics: Diagnostic[] = [];
const remainingProperties = new Map(source.properties);

for (const prop of walkPropertiesInherited(target)) {
const sourceProperty = getProperty(source, prop.name);
if (sourceProperty === undefined) {
Expand All @@ -5735,6 +5741,8 @@
);
}
} else {
remainingProperties.delete(prop.name);

const [related, propDiagnostics] = isTypeAssignableToInternal(
sourceProperty.type,
prop.type,
Expand All @@ -5746,6 +5754,30 @@
}
}
}

if (target.indexer) {
const [_, indexerDiagnostics] = arePropertiesAssignableToIndexer(
remainingProperties,
target.indexer.value,
diagnosticTarget,
relationCache
);
diagnostics.push(...indexerDiagnostics);

// For anonymous models we don't need an indexer
if (source.name !== "" && target.indexer.key.name !== "integer") {
const [related, indexDiagnostics] = areIndexAreCompatible(
source,
target as any,
diagnosticTarget,
relationCache
);
if (!related) {
diagnostics.push(...indexDiagnostics);
}
}
}

return [diagnostics.length === 0 ? Related.true : Related.false, diagnostics];
}

Expand All @@ -5756,7 +5788,57 @@
);
}

function arePropertiesAssignableToIndexer(
properties: Map<string, ModelProperty>,
indexerConstaint: Type,
diagnosticTarget: DiagnosticTarget,
relationCache: MultiKeyMap<[Type, Type], Related>
): [Related, readonly Diagnostic[]] {
for (const prop of properties.values()) {
const [related, diagnostics] = isTypeAssignableToInternal(
prop.type,
indexerConstaint,
diagnosticTarget,
relationCache
);
if (!related) {
return [Related.false, diagnostics];
}
}

return [Related.true, []];
}

function areIndexAreCompatible(
timotheeguerin marked this conversation as resolved.
Show resolved Hide resolved
source: Model,
target: Model & { indexer: ModelIndexer },
diagnosticTarget: DiagnosticTarget,
relationCache: MultiKeyMap<[Type | ValueType, Type | ValueType], Related>
): [Related, readonly Diagnostic[]] {
if (source.indexer === undefined || source.indexer.key !== target.indexer.key) {
return [
Related.false,
[
createDiagnostic({
code: "missing-index",
format: {
indexType: getTypeName(target.indexer.key),
sourceType: getTypeName(source),
},
target: diagnosticTarget,
}),
],
];
}
return isTypeAssignableToInternal(
source.indexer.value!,
target.indexer.value,
diagnosticTarget,
relationCache
);
}

function isIndexerValid(

Check warning on line 5841 in packages/compiler/src/core/checker.ts

View workflow job for this annotation

GitHub Actions / Lint

'isIndexerValid' is defined but never used. Allowed unused vars must match /^_/u
source: Model,
target: Model & { indexer: ModelIndexer },
diagnosticTarget: DiagnosticTarget,
Expand Down
13 changes: 13 additions & 0 deletions packages/compiler/test/checker/relation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,19 @@ describe("compiler: checker: type relations", () => {
});
});

it("type with spread indexer allow other properties to no match index", async () => {
await expectTypeAssignable({
source: "{age: int32, other: string}",
target: "Foo",
commonCode: `
model Foo {
age: int32;
...Record<string>;
}
`,
});
});

it("emit diagnostic assigning other type", async () => {
await expectTypeNotAssignable(
{ source: `string`, target: "Record<string>" },
Expand Down
Loading