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

[Bug] Inferred types break with certain target types #148

Closed
C-Higgins opened this issue Nov 11, 2019 · 2 comments
Closed

[Bug] Inferred types break with certain target types #148

C-Higgins opened this issue Nov 11, 2019 · 2 comments

Comments

@C-Higgins
Copy link

C-Higgins commented Nov 11, 2019

When the target type is certain combinations of union types, I think when the unions share a property, the type of the ActionFunction breaks and no longer knows the types of the parameters.

** Reproduce **

const test = createSchema<{a:string} | {a:string; b:string},{c:string}>({
  a: it => {} // Parameter 'it' implicitly has 'any' type
  b: it => {} // here 'it' is the correct type
})

As you can see, it should be of type {c:string}, for the first property, but it is not. The other parameters are also any. If you remove a from the second type, it works as expected.

@emyann
Copy link
Member

emyann commented Nov 11, 2019

@C-Higgins Thanks for taking the time to report this! Sounds like a weird narrowing/widening issue with Typescript Union Type being processed recursively by:

export type StrictSchema<Target = any, Source = any> = {
  /** `destinationProperty` is the name of the property of the target object you want to produce */
  [destinationProperty in keyof Target]:
    | ActionString<Source>
    | ActionFunction<Target, Source, Target[destinationProperty]>
    | ActionAggregator<Source>
    | ActionSelector<Source, Target>
    | StrictSchema<Target[destinationProperty], Source>;

export interface ActionFunction<D = any, S = any, R = any> {
  (iteratee: S, source: S[], target: D): R;
}

Let me dig this a bit, and come back to you when I have a clue :)

Edit: this is definitely a typescript widening behaviour

replacing

export type StrictSchema<Target = any, Source = any> = {
  /** `destinationProperty` is the name of the property of the target object you want to produce */
  [destinationProperty in keyof Target]:
    | ActionFunction<Target, Source, Target[destinationProperty]>
    ....

by

export type StrictSchema<Target = any, Source = any> = {
  /** `destinationProperty` is the name of the property of the target object you want to produce */
  [destinationProperty in keyof Target]:
    | (iteratee: Source, source: Source[], target:  Target[destinationProperty]) => Target[destinationProperty]

solves the issue 🙃

@emyann
Copy link
Member

emyann commented Nov 11, 2019

@C-Higgins This issue should have been fixed in #149 which is version 1.12.2, please let me know if you encounter any issues of this type again :)

https://repl.it/@yrnd1/GlitteringPleasedEquations

@emyann emyann closed this as completed Nov 18, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants