最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

reactjs - optional react useState types with jsdoc, typescript checkjs (javascript) - Stack Overflow

programmeradmin4浏览0评论

using typescript's jsdoc support to type the following javascript code:

const [optionalNumber, setOptionalNumber] = useState(null)

const handleClick = () => {
  setOptionalNumber(42) 
  //          ^-- Argument of type '42' is not assignable to parameter of type 'SetStateAction<null>'
}

the way i currently get around this works but is a bit ugly:

const [optional, setOptional] = useState(
  /** @type {number|null} */ (null)
)

how can i acplish this without using casting? i want optional to have a type of null | number, and setOptional to only accept null | number as an argument.

codesandbox demonstrating this:

using typescript's jsdoc support to type the following javascript code:

const [optionalNumber, setOptionalNumber] = useState(null)

const handleClick = () => {
  setOptionalNumber(42) 
  //          ^-- Argument of type '42' is not assignable to parameter of type 'SetStateAction<null>'
}

the way i currently get around this works but is a bit ugly:

const [optional, setOptional] = useState(
  /** @type {number|null} */ (null)
)

how can i acplish this without using casting? i want optional to have a type of null | number, and setOptional to only accept null | number as an argument.

codesandbox demonstrating this:
https://codesandbox.io/s/optimistic-villani-kbudi?fontsize=14

Share Improve this question edited Oct 9, 2019 at 22:01 schpet asked Oct 9, 2019 at 21:29 schpetschpet 10.6k6 gold badges35 silver badges36 bronze badges 4
  • Just use undefined instead of null: const [optional, setOptional] = useState() – Jared Smith Commented Oct 9, 2019 at 21:30
  • i still want this limited to {null|number} - useState with no args results in optional being any – schpet Commented Oct 9, 2019 at 21:33
  • updated with more context about this – schpet Commented Oct 9, 2019 at 21:35
  • 1 It's not currently possible... there is an open issue for it: github./microsoft/TypeScript/issues/27387 – Kyle Commented Mar 20, 2020 at 6:03
Add a ment  | 

2 Answers 2

Reset to default 7

I think you can define an initial value with the correct union type and pass it to useState.

It would be something like this:

/**
* @type {number | null}
*/
const initValue = 42;
const [optionalNumber, setOptionalNumber] = useState(initValue)

Assuming that your ponent relies on the inital state of the optional state being the null value (rather than undefined), one solution would be to explicitly specify the state hooks type as a union type of both number and null like so:

// Allows initial value to be null, and number to be subsequently set 
const [optional, setOptional] = useState<number | null>(null);

// optional === null

setOptional(42);

Alternatively, if your ponent does not distingush between undefined or null for the inital optional state value, then the following would work:

const [optional, setOptional] = useState<number>();

// optional === undefined

setOptional(42);
发布评论

评论列表(0)

  1. 暂无评论