-
Notifications
You must be signed in to change notification settings - Fork 4.9k
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
Conversation
return [sectionId, defaultOption]; | ||
}), | ||
); | ||
|
||
return map; | ||
return map as Record<SectionId, ParameterMappingOptions>; |
There was a problem hiding this comment.
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>
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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
|
@@ -64,7 +64,7 @@ function getParameterOperatorType(parameterType?: string) { | |||
|
|||
export function buildTypedOperatorOptions( | |||
operatorType: OperatorType, | |||
sectionId: string, | |||
sectionId: "number" | "string" | "date" | "location" | "id", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sectionId: "number" | "string" | "date" | "location" | "id", | |
sectionId: SectionId, |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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); |
There was a problem hiding this comment.
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>; |
There was a problem hiding this comment.
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.
closes #42196
Provide better types instead of general
string
forSectionId
.How to verify