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

no-object-as-default-parameter should ignore for objects with required parameters #2199

Open
ST-DDT opened this issue Oct 9, 2023 · 0 comments
Labels
bug help wanted types Issues that happen in TypeScript or that require types

Comments

@ST-DDT
Copy link
Contributor

ST-DDT commented Oct 9, 2023

Sometimes a method has a required set of option parameters. e.g. min and max.

function valueBetween(range: {min: number, max: number} = { min: 0, max: 10 }) {} ✔️

In this case it should be OK to use object default parameters because both min and max are required.
Or to be more precise, it should be ok if defined default properties === required properties.
It is not OK if any optional property is provided.

Yes, it it possible for plain JS users to fall through, but they are also likely to fail with the types in general or cause conflicts due to mismatching boundaries:

valueBetween({min: 'a', max: 10}); 
valueBetween({min: 100}); 

Otherwise it is impossible to implement the method parameter default in a type safe way.

function valueBetween(range: {min: number, max: number} = {} ) { // ts-error
    const { min = 0, max = 10 } = options;
}

https://github.com/sindresorhus/eslint-plugin-unicorn/blob/main/docs/rules/no-object-as-default-parameter.md

function valueBetween(range: {min: number, max: number} = { min: 0, max: 10 }) {} :check ✔️
function valueBetween(range: {min: number, max: number, step: number} = { min: 0, max: 10 }) {} 
function valueBetween(range: {min: number, max: number, step?: number} = { min: 0, max: 10, step: 1 }) {} 

https://github.com/faker-js/faker/blob/fe3fb5dd3b8949d952aa8eb3830f58fc9cc4adcc/src/modules/lorem/index.ts#L377

@fregante fregante changed the title Ignore no-object-as-default-parameter for objects with required parameters no-object-as-default-parameter should ignore for objects with required parameters Feb 10, 2024
@fregante fregante added bug help wanted types Issues that happen in TypeScript or that require types labels Feb 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted types Issues that happen in TypeScript or that require types
Projects
None yet
Development

No branches or pull requests

2 participants