Bug: Behavior in tsplayground and local project differ

This issue has been created since 2022-09-09.

React version: 18.1.0

Steps To Reproduce

  1. Install [email protected] [email protected] [email protected] and the respective @types
  2. Create the following component with props
type FooProps = {
  base: string;
} & (
  | {
      cb: (n: number) => void;
      flag: true;
    }
  | {
      cb: (n: [number]) => void;
      flag: false;
    }
);

const Foo: React.FC<FooProps> = ({ cb, flag }) => {
  const on = (n: number | [number]) => {
    if (flag && Array.isArray(n)) {
      cb(n);
    } else if (!flag && !Array.isArray(n)) {
      cb(n);
    }
  };
  return null;
};
  1. Then on the line with cb(n) the types aren't inferring correctly. If you put this code in a tsplayground it works

Link to code example:

Here's a tsplayground and a codesandbox

The current behavior

The type for n isn't inferring correctly. This happens in codesandbox and vscode locally.

The expected behavior

Same behavior as what happens in the tsplayground which is that the type is correctly narrowed down.

eps1lon wrote this answer on 2022-09-10

Support requests filed as GitHub issues often go unanswered. We want you to find the answer you're looking for, so we suggest the following alternatives:

Coding Questions
If you have a coding question related to React and React DOM, it might be better suited for Stack Overflow. It's a great place to browse through frequent questions about using React, as well as ask for help with specific questions.

https://stackoverflow.com/questions/tagged/react

Talk to other React developers
There are many online forums which are a great place for discussion about best practices and application architecture as well as the future of React.

https://reactjs.org/community/support.html

More Details About Repo
Owner Name facebook
Repo Name react
Full Name facebook/react
Language JavaScript
Created Date 2013-05-24
Updated Date 2022-09-27
Star Count 195296
Watcher Count 6655
Fork Count 40431
Issue Count 1113

YOU MAY BE INTERESTED

Issue Title Created Date Comment Count Updated Date
Possible incorrect documentation? 1 2022-04-26 2022-09-13
[BUG] Contours Tool not working after Alt Tab 4 2021-07-02 2022-08-30
[Issue] Hello, when I enter the user list, I get the following error. I have role permission 3 2022-05-02 2022-07-14
FATAL ERROR: MarkCompactCollector: young object promotion failed Allocation failed - JavaScript heap out of memory 2 2021-02-11 2022-08-31
BA.1* sublineage with spike P9S, I128V, S151del, W152R, R190T, R346T, F490S, D936Y, and possibly N370K, V445A and N460K (4 seqs as of 2022-08-31, Peru) 4 2022-09-02 2022-09-23
[BUG] Cannot remove student from class 12 2022-01-31 2022-08-08
[BUG] Explore crashes when filtering without being logged in 0 2022-01-31 2022-08-31
[UI idea] Give modal alert a non-shared timeout 0 2022-01-31 2022-09-24
Typescript type missing!!! 4 2019-05-13 2022-09-25
ValueError: Shapes (18,) and (255,) are incompatible 5 2020-05-25 2022-09-10
is this change Shortcut? 1 2022-05-24 2022-09-22
설치 버전을 만들 수 있습니까? 지금은 이렇게 소프트웨어를 열 때 속도가 매우 느리다 0 2022-05-25 2022-09-20
English Documentation 0 2021-12-30 2022-09-20
Huawei system updater 0 2022-04-03 2022-09-04
DownloadLengthMismatchError because requests automatically decodes gzip files 5 2022-07-04 2022-09-02
Could not load boards list 1 2022-01-24 2022-08-15
What is the take on error handling, multithreading, etc.. 5 2021-02-10 2022-09-08
Studio: Unable to maintain ordering of dashboards in a workspace 0 2022-05-30 2022-08-31
Missing configuration options in Investigation menu 0 2021-11-03 2022-09-18
2.7.2 移除未引用ID会导致商店的选择单位(选择英雄)技能丢失图标和技能位置 (Aneu Ane2) 6 2021-12-26 2022-08-19
[MU4 Issue] Figured bass: Different font is displayed during entering 1 2021-12-21 2022-07-27
Warnings after project run 0 2021-03-02 2022-08-09
ssl root certs missing 4 2021-03-24 2022-09-24
Show Timestamp from Events 2 2022-07-15 2022-09-15
Capture and redirect the back button 0 2022-02-23 2022-08-30
Expose ledger events via `foldBlocks` and `chainSyncClientWithLedgerState` 0 2021-11-17 2022-09-08
Typo in WordPress Plugin Options > presets[].options 0 2022-04-20 2022-09-16
IKEA 345lm GU10 (LED1923R5/TRADFRI bulb GU10 CWS 380lm) 2 2022-02-07 2022-09-12
RTK Query fails only on node v14 SSR node-fetch calls 3 2022-01-29 2022-09-23
jsonpContainer-2.0 and jsonbContainer-2.0 features incorrectly use default providers. 1 2022-02-16 2022-09-24
Add a comment field to the (un)blacklist of website 0 2022-01-09 2022-09-26
Add custom invoice field in percent 0 2022-04-10 2022-09-25
Create initiative fails 1 2019-12-17 2022-08-25
Resolve Missing Javadocs in Resource Manager SDKs on resourcemanagerhybrid 1 2021-11-24 2022-09-20
版本1.0升级到2.0 使用vite编译报错的问题 does not provide an export named 'withRouter' 2 2021-12-03 2022-09-25
Simd sgradient 1 2018-06-22 2022-01-03
CIS194's online edition 404 NOT Found now 2 2022-06-30 2022-08-15
Spotify integration fails to setup when using NabuCasa 3 2022-02-16 2022-03-03
Hue Integration stopped working after updating to version 2022.2.7 1 2022-02-16 2022-03-06
Issue with default export in typescript definitions 6 2022-04-16 2022-09-16
小白提问,udp转发是否会导致被识别 19 2020-07-24 2022-01-15
[Question] Server shuts down connection abruptly 0 2021-12-20 2022-09-05
JOB快速执行脚本插件-脚本内容处建议增加全屏放大功能 0 2022-06-22 2022-09-24
Vault using postgresql database, sometimes not un-initialises the vault by truncating vault_kv_store and vault_ha_locks 0 2022-05-20 2022-09-19
Fresh install of 7.9.1 fails 2 2020-09-16 2022-09-15
VersionedIntervalTimeline.isOvershadowed is very slow when processing many segments 7 2021-09-13 2022-09-17
4.2.4 Delete all maps on sw update is back 1 2022-06-24 2022-09-06
Microservices - Exception Filtering - RPCException not working 2 2022-01-25 2022-09-19
spring.codec.max-in-memory-size does not seem to be honored for JSON decoding 3 2021-08-30 2022-09-26
Perf improvement: json_actix 0 2021-11-13 2022-09-14