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

Improve types for ParameterSettings #42269

Merged
merged 3 commits into from
May 6, 2024

Conversation

uladzimirdev
Copy link
Contributor

@uladzimirdev uladzimirdev commented May 6, 2024

closes #42196

Provide better types instead of general string for SectionId.

How to verify

  • ci is green

return [sectionId, defaultOption];
}),
);

return map;
return map as Record<SectionId, ParameterMappingOptions>;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used as here because Object.fromEntries returns <string, T> and not <SectionId, T>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Object.fromEntries returns (roughly) Partial<Record<SectionId, ParameterMappingOptions>>. TS can't ensure that every SectionId key will be present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤔 maybe I have wrong TS version locally

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My point was that if you remove the cast and explicitly specify that this function returns Partial<Record<SectionId, ParameterMappingOptions>> - TS won't complain (in this file at least).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true, I reworked it a bit e387a47

@uladzimirdev uladzimirdev added the no-backport Do not backport this PR to any branch label May 6, 2024
@uladzimirdev uladzimirdev requested a review from a team May 6, 2024 11:08
Copy link

replay-io bot commented May 6, 2024

Status Complete ↗︎
Commit 6b5f496
Results
⚠️ 9 Flaky
2457 Passed

@@ -64,7 +64,7 @@ function getParameterOperatorType(parameterType?: string) {

export function buildTypedOperatorOptions(
operatorType: OperatorType,
sectionId: string,
sectionId: "number" | "string" | "date" | "location" | "id",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
sectionId: "number" | "string" | "date" | "location" | "id",
sectionId: SectionId,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unfortunately imports from main codebase is restricted in metabase-lib folder

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any other place where we can define SectionId to not hit this problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

inside the metabase-lib 😄 let me fix it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

name: t`Number`,
description: t`Subtotal, Age, Price, Quantity, etc.`,
options: buildTypedOperatorOptions("number", "number", t`Number`),
},
{
id: "string",
id: "string" as const,
name: t`Text or Category`,
description: t`Name, Rating, Description, etc.`,
options: buildTypedOperatorOptions("string", "string", t`Text`),
},
].filter(Boolean);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This filter is redundant

return [sectionId, defaultOption];
}),
);

return map;
return map as Record<SectionId, ParameterMappingOptions>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually Object.fromEntries returns (roughly) Partial<Record<SectionId, ParameterMappingOptions>>. TS can't ensure that every SectionId key will be present.

@uladzimirdev uladzimirdev merged commit 1ff8021 into master May 6, 2024
111 checks passed
@uladzimirdev uladzimirdev deleted the typescript-types-parameter-sections branch May 6, 2024 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
no-backport Do not backport this PR to any branch .Team/QueryingComponents
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add typescript types for parameter sections
2 participants