-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Suggestion: automatically mark named parameters with defaults as optional #30488
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
Comments
I have some hangups about this. I routinely check my required parameters. Just because TypeScript says it is required doesn't mean that constraint is respected on the JavaScrpt side. I personally love the idea of forcing required parameters and providing defaults (along with some more logic) to protect on the JS consumer side. I think making people explicitly state what they want serves the two thoughts fine as is. Also, you are talking about overriding the intended behaviour of a third-party design object. Is that wise? |
I appreciate the symmetry with parameter defaults this would create, but I have probably a dozen objections to this. What if you didn't want the property to be optional, i.e. you want an explicit This is problematic for .d.ts generation when the type annotation isn't an anonymous object type. As a first principle, if the user writes a type annotation, we shouldn't defy that annotation without some extremely good motivation. There's an argument that destructuring default is just sugar for an additional conditional assignment at the top of the function body, which would imply the parameter type shouldn't change, though again this isn't preserved by the bare parameter default behavior. |
We have the same problem, especially for factory constructors. We came with this workaround. Say we have the interface Message: export interface Message {
fieldName?: string;
status: string;
statusText: string;
type: "ERROR" | "SUCCESS" | "WARNING" | "REDIRECT";
userMessage: string;
userTitle: string;
} And you have a factory constructor, that just provides some defaults: export function createMessage({
userMessage = '',
userTitle = '',
status = '',
statusText = '',
...rest
}: OptionalKeys<
Message,
'userMessage' | 'userTitle' | 'status' | 'statusText'
>): Message {
return {
userTitle,
userMessage,
type,
status,
statusText,
...rest,
};
} We use this OptionalKeys type utility, to make sure the default values are marked as optional: export type OptionalKeys<T extends object, K extends keyof T> = Partial<Pick<T, K>> & Omit<T, K>; |
Uh oh!
There was an error while loading. Please reload this page.
Search Terms
default destructuring named parameters object optional automatically
Suggestion
When using "named parameters", if a parameter has a default (provided when destructuring), TypeScript should automatically mark that named parameter as optional. Example:
You might ask "why not just manually mark the parameter as optional?" Good question! Because:
Use Cases
React function components are a very common use case for "named parameters" and destructuring with defaults.
(
defaultProps
is supposed to solve this exact problem, but it doesn't currently work for function components: DefinitelyTyped/DefinitelyTyped#30695).Examples
Checklist
My suggestion meets these guidelines:
The text was updated successfully, but these errors were encountered: