#Typing a Partial-but-without-undefined-values type

27 messages · Page 1 of 1 (latest)

jovial parcel
#
balmy roostBOT
#

@jovial parcel Here's a shortened URL of your playground link! You can remove the full link from your message.

jdelstrother#0

Preview:```ts
type State = {
foo: number
bar: string
}
declare function setState<K extends keyof State>(
state: Pick<State, K>
): void

setState({foo: 1})
setState({bar: "hello"})
// @ts-expect-error
setState({bar: 1})
// @ts-expect-error
setState({bar: undefined})
...```

severe stirrup
#

You need to turn exactOptionalPropertyTypes on in tsconfig

#

by default a?: string and ?: string | undefined are treated as the same type, we can't separate them without eOPT

balmy roostBOT
#

@jovial parcel Here's a shortened URL of your playground link! You can remove the full link from your message.

jdelstrother#0

Preview:```ts
// @exactOptionalPropertyTypes: true

type State = {
foo: number;
bar: string;
}
declare function setState<K extends keyof State>(state: (Pick<State, K>)): void;

setState({ foo: 1 });
setState({ bar: 'hello' });
// @ts-expect-error
setState({ bar: 1 });
...```

severe stirrup
#

Otherwise you have to modify your system to deal with undefined

#

Seems you have an issue with your setState type there

#

The generic is weird. K gets inferred as keyof State, so it wants all props

balmy roostBOT
#
sandiford#0

Preview:```ts
// @exactOptionalPropertyTypes: true

type State = {
foo: number;
bar: string;
}
declare function setState(state: Partial<State>): void;

setState({ foo: 1 });
setState({ bar: 'hello' });
// @ts-expect-error
setState({ bar: 1 });
// @ts-expect-error
setState({ bar: undefined });
...```

jovial parcel
#

Bah, that's unfortunate. I guess I'm stuck until that makes it upstream to DefinitelyTyped then

severe stirrup
#

It's a react app?

jovial parcel
#

Yeah. My playground was trying to simplify it down to the bare minimum. You think it's oversimplified?

severe stirrup
#

I just find that typing weird

#

Let's try to use the real react types

#

I guess there is more going on in the real world

#

The solution is going to be to use a concrete type for stateUpdate I think

balmy roostBOT
#
sandiford#0

Preview:```ts
type State = {
foo: number
bar: string
}
declare function setState<K extends keyof State>(
state: Pick<State, K>
): void

setState({foo: 1})
setState({bar: "hello"})
// @ts-expect-error
setState({bar: 1})
// @ts-expect-error
setState({bar: undefined})
...```

severe stirrup
#

I think the way that setState is typed just doesn't play nicely with a Partial

balmy roostBOT
#

@jovial parcel Here's a shortened URL of your playground link! You can remove the full link from your message.

jdelstrother#0

Preview:```ts
// @exactOptionalPropertyTypes: true

import * as React from "react";

type Props = { a: number }
type State = { foo: number, bar: string }

class Foo extends React.Component<Props, State> {
blah() {
let stateUpdate: Partial<State>; // <--- What's a better type for this?
...```

#
sandiford#0

Preview:```ts
// @exactOptionalPropertyTypes: true

import * as React from "react";

type Props = { a: number }
type State = { foo: number, bar: string }

class Foo extends React.Component<Props, State> {
blah() {
if (Math.random() > 0.5) {
const stateUpdate = { foo: 1 }
...```

severe stirrup
#

You can't do this?

severe stirrup
#

Yeah it's perfectly safe I think. So it is possble just to cast it to make react accept it

#

Or you might be able to override some types somewhere, I forget exactly how