stdlib
stdlib copied to clipboard
broken type inference
Description
When using objectValues() and omit() with my typescript application, I lose type inference. I would like to use omit on parts in this function:
const addressPartsRemover = (source: string, parts: AddressParts) =>
Object.values(parts).reduce((acc, part) => acc.replace(part, ''), source);
But then I can not infer types and have to do things such as:
(Object.values(omit(parts, 'house')) as string[]).reduce((acc, part) => acc.replace(part, ''), source);
(objectValues(parts) as string[]).reduce((acc, part) => acc.replace(part, ''), source);
I tired explicitly installing the the types package and added it to my compiler options:
{
"compilerOptions": {
"typeRoots": [ "./node_modules/@stdlib/types" ],
},
}
Related Issues
There's a few type related issues and PRs but I don't think they are related.
Questions
User error? Am I suppose to do something else to use with TypeScript.
Demo
No response
Reproduction
No response
Expected Results
No response
Actual Results
No response
Version
"@stdlib/stdlib": "^0.0.96", "@stdlib/types": "^0.0.14",
Environments
Node.js
Browser Version
No response
Node.js / npm Version
18
Platform
GNU/Linux
Checklist
- [X] Read and understood the Code of Conduct.
- [X] Searched for existing issues and pull requests.
:tada: Welcome! :tada:
And thank you for opening your first issue! We will get back to you shortly. :runner: :dash:
@Planeshifter I think we can tighten up the types for both @stdlib/utils/values and @stdlib/utils/omit.
For @stdlib/utils/values, we can possibly use generics; see https://github.com/stdlib-js/stdlib/blob/232d92dcd2f07187ab1973d88e25cf5354a97633/lib/node_modules/%40stdlib/utils/values/docs/types/index.d.ts#L40. So, instead of returning Array<any>, we return Array<T>.
For @stdlib/utils/omit, we can set the return value as an interface; see https://github.com/stdlib-js/stdlib/blob/232d92dcd2f07187ab1973d88e25cf5354a97633/lib/node_modules/%40stdlib/utils/omit/docs/types/index.d.ts#L43. Here, given that we're only looking at own property keys (which, by virtue of Object.keys, are only strings, not symbols), we know that the returned object must satisfy [key: string]: T. The input obj cannot be any, as specified in the type declaration. It must be sthg which satisfies typeof equal to object, so primitives are excluded, and further, null is not supported. In which case, we can probably make omit a generic, as well, where the input obj can be specified according to an interface with property values of type T.
@kgryte Just to note, Typescript already provides an utility type Omit. https://www.typescriptlang.org/docs/handbook/utility-types.html#omittype-keys
Looks like an exact use case for this function.
@hijarian From the docs, the Omit utility type seems to be for instances where you know the omitted keys AOT. In this instance, the keys are dynamic, so not sure if the Omit type applies here.
@Planeshifter Do you have an update on this?
This is now hopefully addressed by https://github.com/stdlib-js/stdlib/commit/10ab1e71cfce96df534b70874166890754b7d839 and https://github.com/stdlib-js/stdlib/commit/a23de41de4280e8d1291a87c50af5c07e9b5ae54. Notice that for the omit function, overloads are used to support both the use-case where the keys are dynamic and not available at compile time (in which properties of the given type are set to optional via Partial and the other case with static keys where the Omit TS utility can be used.
Thanks, @Planeshifter! I'll go ahead and close. This should be available in the next stdlib release and will also be available in the affected standalone packages. If inference is still broken, we can open a new issue.