Open todor-a opened 4 months ago
This is just #19513 and therefore likely to be working as intended? Note that above where it says "Code" you're supposed to put it in there as plaintext.
This is a sort of awkward interaction between the implied-optional properties and the index signature. Two good workarounds are either to write params?: Partial<Record<string, FilterParamValue>>;
, or turn on exactOptionalProperties
. I don't really see a better fix except maybe to synthesize the optional properties with type never
under EOPT: false, which would probably have fairly awful consequences. It'd be interesting to evaluate a PR for that.
Simpler repro:
type Foo = {
stuff: Record<string, number>;
};
function getFoo() {
if (Math.random() > 0.5) {
return { stuff: { a: 42 } };
} else {
return { stuff: { b: 99 } };
}
}
// Actually fine, but claimed not to be
const p: Foo = getFoo();
🔎 Search Terms
switch map array narrow
🕗 Version & Regression Information
I went back to TS 4 - still present
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/KYDwDg9gTgLgBDAnmYcBiBLANjYUAKAhlIQLYBqhWArqgLxwDOMUGAdgOZwA+cb1pAEZ4ecACKFco5q04BtALoBuALAAodaEiw47XFABmhAMaoAkphx4APABU4oXGwAmjJi3YcAfHADe6uEC+MmAALjhbVTUguDBiMkYAfnCAJWBjaGdrGU8AGnRsfSISCipaLyiAX3VNcGh4PTwjUzgzACFCRgxjS30AZWAOUmA2GDsHECdXd1lvCam3C0KbWx9-aKCkFHCAcgNlqB2qmrUteoRkc168AaGRsftHEemczh8GJasoW+HRgFFwFBgIwuhA2HYfLx2p1utdvoNfg8KrVtPAtqgfvcAPIoEgwaBwBg7QguHaiHbQI4o86NQwmK4HTH-QHA0Hgx6TZ5uV5zdYxdHhJkwHF4STQKIxMBA5zdSTA8KffoI+4QxTHDSnfikApfAByITcfgCQQ6UDMbiJgmIAH0MIwdrljYEOgAvc2EuA7K0u2329TVDUjATiDC-Nm2S5GjbO4ger3EB1OuCuuPenb+k5nHTo1piUMjNkevlBDDOcI8iWbS7hPNhjBgiMoAB0pqqomLgVL5Y8nErgSwhGEWG7sz7F22IbrDcuLcILqqUXUGTYzAQJBXBmg2oYAAoZVOVwrawX62xFABKBVwoXWOH64aMd5rJNAmDUKBsKMxfnVz0k5yJtG35SsAMrGHKjDhPuJ5gowTakIQYB7vmK6noSz5Ad+QSMAA7hgMDGAAFnAyEHk26Lnl+WHUYE4GMKgx6odOzamuEHY0Rxr7vp+7EcXx45hJ6+xfIB-FicEwzhHeBqzmajCOph4lYXEJSQVRSliVaZplnA0FMWwTalkmGnfgGJkxGZGmWUpdEMSh4Yzq6bHGRpXEfup5k-hOewHKJnnfmwIRSQc97ArObryS5nkqQkzmKf5QTemYOl6WyhnOFF5nWVlmU0dleWZZU55JgGlRAA
💻 Code
🙁 Actual behavior
A compile-time error is displayed. It appears that the fact that we might return in the first switch case is "carried over" to the second one.
🙂 Expected behavior
Type narrowing to work correctly as the mapping is correct. In fact, if one extracts this mapping to a separate function, the behaviour is fixed. Playground.
Additional information about the issue
No response