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

Payload type unknown when defining optional property? #214

Closed
mcchrish opened this issue Oct 15, 2019 · 5 comments
Closed

Payload type unknown when defining optional property? #214

mcchrish opened this issue Oct 15, 2019 · 5 comments

Comments

@mcchrish
Copy link

This is for version 0.8.0.

const action = createAction<{ input?: string }>('ACTION')
type ActionType = ReturnType<typeof action> // ActionType.payload === unknown


const action = createAction<{ input?: string, input2: string }>('ACTION')
type ActionType = ReturnType<typeof action> // ActionType.payload === { input?: string | undefined; input2: string }

Somehow, if there is only one property and it's optional, it converts it type to unknown

@markerikson
Copy link
Collaborator

markerikson commented Oct 15, 2019

Oh dear. What version of TS are you on?

@phryneas , @Jessidhia : any ideas?

@phryneas
Copy link
Member

Phew. Yup.

The feature detection for TS<3.5 was leaking both cases (TS<3.5 and TS>=3.5), exposing the pre-3.5 behaviour.
Back then, we could not distinguish between the empty object default case and unknown, so it would fall back to unknown behaviour if the object looked like an empty object (which is the case here).

We can fix this for TS>=3.5, but in old versions this will be the behaviour if something possibly looks like an empty object.

( fixed TS version feature detection would look like this:

export type AtLeastTS35<True, False> = [True, False][IsUnknown<ReturnType<<T>() => T>, 0, 1>]

)

Question is: is it worth it supporting old TS versions, which should be our minimum requirement? I guess we should still support 3.4, which is from march 2019, even if it hurts a little :/

@markerikson
Copy link
Collaborator

Yeah, 3.4 seems reasonable to support, even if it's a bit of a pain.

@phryneas
Copy link
Member

I might be even able to fix it for pre-3.5... PR coming in.

@phryneas phryneas mentioned this issue Oct 15, 2019
markerikson pushed a commit that referenced this issue Oct 16, 2019
* fix(createClice) fix a bug with TS 3.6 (see #212)

* fix(types) fix feature detection for TS<3.5, make IsEmptyObj more accurate

* chore: any-cast NonInferred type

* Formatting
@markerikson
Copy link
Collaborator

Fix published as 0.8.1. Hopefully that should resolve this - let us know if there's still an issue!

markerikson added a commit that referenced this issue Apr 20, 2021
Co-authored-by: Matt Sutkowski <msutkowski@gmail.com>
Co-authored-by: Rob Clayburn <rob@infosum.com>
Co-authored-by: Lenz Weber <mail@lenzw.de>
Co-authored-by: Lenz Weber <lenz.weber@mayflower.de>
Co-authored-by: kahirokunn <okinakahiro@gmail.com>
Co-authored-by: DmitryScaletta <DmitryScaletta@users.noreply.github.com>
Co-authored-by: Guillaume FORTAINE <guillaume+github@fortaine.com>
Co-authored-by: triou <34629112+tevariou@users.noreply.github.com>
Co-authored-by: Rob Clayburn <rob@pollen-8.co.uk>
Co-authored-by: Shrugsy <joshfraser91@gmail.com>
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

3 participants