Closed kabirsky closed 1 year ago
Thanks for report this!
But with {}
there's eslint error:
Don't use `{}` as a type. `{}` actually means "any non-nullish value".
- If you want a type meaning "any object", you probably want `Record<string, unknown>` instead.
- If you want a type meaning "any value", you probably want `unknown` instead.
- If you want a type meaning "empty object", you probably want `Record<string, never>` instead.
So how about P extends any
?
Thanks for report this! But with
{}
there's eslint error:Don't use `{}` as a type. `{}` actually means "any non-nullish value". - If you want a type meaning "any object", you probably want `Record<string, unknown>` instead. - If you want a type meaning "any value", you probably want `unknown` instead. - If you want a type meaning "empty object", you probably want `Record<string, never>` instead.
So how about
P extends any
?
But {}
already allowed in .eslintrc:
'@typescript-eslint/ban-types': [ 'error', { extendDefaults: true, types: { '{}': false, }, } ],
Still, let me outline available (and not) variants for this case:
<P>
, which is an alias for <P extends unknown>
- this variant is unavailable since we cant destructure something that might not be an object, and tsc will throw an error<P extends any>
has pretty much same problem - since P
can be anything it could be not an object, which is incorrect in our case and tsc will throw an error<P extends {}>
means 'P is an object', and in older versions of Typescript <P>
would be an alias for <P extends {}>
, because this type is quite broad, but still removes all primitive and nullish values from our scope<P extends Record<string, unknown>>
is as close as we can get to the truth, but since interfaces can be augmented, AND with a non-string key - this type break all interface-oriented code. I am all for using type
, but I think we should support all typescript syntax, and in this case it is such an easy taskSo, after all considerations, I still think <P extends {}>
is the only type that will give us meaning we need that will not unnecessarily break user's code
Published in v1.2.13
. Thanks @kabirsky !
I was hit with interesting type of regression after updating lib from version 1.2.8 After some investigation I found, that interface are not legal extension of Record<string, unknown>, and this is by design (per https://github.com/microsoft/TypeScript/issues/15300#issuecomment-332366024), since they are potential target of interface merge Since I don't see any real improvement over
P extends {}
I thought it would be better to revert this particular change