Skip to content

Conversation

@victorgarciaesgi
Copy link
Collaborator

This PR is targeted for oneOfType.

Problem before:

Before vue-types

props: {
   foo: { type: [String, Object] as PropType<RawLocation> }
}

With vue-types ☹️

image

With these changes, we can now cast the type we want and still have the default behaviour!

type RawLocation = string | { name: string }
const ctor = oneOfType([String, Object]) // Valid
const route = oneOfType<RawLocation>([String, Object]) // Valid

Screenshot at Jul 12 21-00-15

Screenshot at Jul 12 21-00-28

@victorgarciaesgi
Copy link
Collaborator Author

Also worth notting, it could be better if U and V could still be infered to check the liability of the casting, but Typescript is blocking us since 2016 microsoft/TypeScript#10571 😩

@dwightjack dwightjack self-assigned this Jul 12, 2021
@dwightjack dwightjack changed the base branch from main to ts-args-for-natives July 13, 2021 04:42
@dwightjack
Copy link
Owner

Thanks!

I've changed the base branch to ts-args-for-natives so that I can merge it and add the related documentation.

@dwightjack dwightjack merged commit 4ecf85b into dwightjack:ts-args-for-natives Jul 13, 2021
@dwightjack
Copy link
Owner

dwightjack commented Jul 13, 2021

@victorgarciaesgi Sorry for commenting on a closed PR.

After merging, I found that these changes create some type issues with the .def() method:

const ctor = oneOfType([String, Object]) // Valid
ctor.def('hello') // Argument of type 'string' is not assignable to parameter of type 'StringConstructor | ObjectConstructor'

Any hint on how to fix it?

@victorgarciaesgi
Copy link
Collaborator Author

@dwightjack Oh shit sorry I will look at it right now!

@victorgarciaesgi
Copy link
Collaborator Author

@dwightjack I fixed it on this branch

@victorgarciaesgi
Copy link
Collaborator Author

Do you want me to make another PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants