-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Regression in nightly build related to keyof, type narrowing, and exactOptionalPropertyTypes #55756
Comments
The change between origin/release-5.2 and origin/main occurred at e6321d7. |
Uh, no |
This seems like a correct change. The example is not, from TypeScript's perspective, any different from let m1: keyof IAllOptions = 'propertyA' as keyof IAllOptions;
let m2: keyof IAllOptions = 'propertyB' as keyof IAllOptions;
dest[m1] = src[m2]; which should be an error (and also changed at the same time). That said, we should narrow down which PR did this to make sure it was at least somewhat intended. |
I'm like 99% positive this has come up before in different contexts but basically, yeah, the compiler only sees the types in zero-order and doesn't understand the higher-order concept that
@RyanCavanaugh Looks like that'd be #55585, if typescript-bot's updated bisect is correct (the commit you quoted is different from the one that's linked now). |
I can't say this one was intended but it also looks to me like a correctness improvement. What I can offer is to add some tests covering this but other than that LGTM and the issue could be closed. |
Adding an explicit |
@SlurpTheo I’d recommend opening a new issue for that because it definitely sounds like a bug. |
I can't come up with a reproduction that doesn't involve |
Yeah, that's expected; once you have an |
#30769 was a similar change made years ago that was likewise a big source of consternation when first introduced, for what it's worth. |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Whoa 👀 type T = Record<string, unknown>;
function f3<U extends T>(p: { objT: T; objU: U }) {
p.objT["foo"] = 11;
// !!! I'm not sure I understand !!!
// @ts-expect-error ts(2536): Type '"foo"' cannot be used to index type 'U'.
p.objU["foo"] = 22;
// @ts-expect-error ts(2352): Property 'foo' is missing in type 'T' but required in type '{ foo: unknown; }'.
(p.objU as { foo: unknown })["foo"] = 222;
(p.objU as { foo?: unknown })["foo"] = 2222;
(p.objU as T)["foo"] = 22222;
}
f3({ objT: {}, objU: {} });
f3<{ bar: unknown }>({
objT: { baz: 33 },
objU: { bar: 44 },
});
f3<{ bar: unknown }>({
objT: { baz: 33 },
objU: {
bar: 44,
// @ts-expect-error ts(2322): Object literal may only specify known properties, and 'qux' does not exist in type '{ bar: unknown; }'.
qux: 55,
},
});
const objU1 = {
bar: 44,
qux: 55,
};
f3<{ bar: unknown }>({
objT: { baz: 33 },
objU: objU1,
});
const objU2 = {
qux: 55,
};
f3<{ bar: unknown }>({
objT: { baz: 33 },
// @ts-expect-error ts(2741): Property 'bar' is missing in type '{ qux: number; }' but required in type '{ bar: unknown; }'.
objU: objU2,
}); Setting properties on |
I mean that pull request does state the following:
But it still makes me 😵 |
🔎 Search Terms
keyof
type narrowing
exactOptionalPropertyTypes
🕗 Version & Regression Information
⏯ Playground Link
https://www.typescriptlang.org/play?exactOptionalPropertyTypes=true&ts=5.3.0-dev.20230915#code/KYDwDg9gTgLgBASwHY2FAZgQwMbDgSQCFMBnPAbwFgAoASAH0AjKTJAEwC45WBPAbhoBfGjVCRYiFGiy4CAETQIAbsDYBBOKFTsSBYmThU6YKBDBoYPNQH4ujCBAA2wVgOrDqo8NHjJUGHDx8BShlVUJNEG02XSJSChpaEzMLHkJbOBIYUKQAczcPL3F4S3MCNUdHAHkwGAQIJF0AXj14uAAyRODFFXUOrpCwtkI3Ip84dABXJGw6hrhsRwbgGrnGgApEkihsLnwK6tr6xoAaRLZgLLgW4ABbWp5V45IAHn3Kp4aSAD51gEoztQ-oZEusqowAFbAWYAOgA1sAeCR1ttsMDSHB1gieBB0OUPkcvn8ANoAXT+MPQ0AAojgABbrMDXb4gui0BB49alYC4zI7YlgUlwACETRaACIIJDoTBxcCjLRFRcsgKhS1Uaq3IqPLRBH9RnQoMAYJMoEg4MqYAURNQpjM1pp7pZPo0XgAVSLRWIHF0-f5cD0Ko0ms2GOBMFjsLhKCAINhwAAMcEE3F0boKQA
💻 Code
🙁 Actual behavior
There is an error on the line attempting to copy
src[p]
intodest[p]
:🙂 Expected behavior
Previously this compiled without error. Both objects are of the same type, so they should have the same properties.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: