You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
"Object literal may only specify known properties" doesn't work on arrays #52219
This seems to be an unintended behavior of the compiler.
This is the behavior in every version I tried (Latest release & nightly), and I reviewed the FAQ for entries about
⏯ Playground Link
typeTextureUse=|{edition: "bedrock"|"dungeons"}|{edition: "java";type: "resource_pack";assets: string}|{edition: "java";type: "texture_pack"};letuses: TextureUse[]=[{edition: "java",type: "texture_pack",assets: "foo",// OK, the error was expected},{edition: "java",type: "resource_pack",assets: "minecraft",},{edition: "bedrock",type: "resource_pack"// NOT OK,// -> Shouldn't be allowed as TextureUse doesn't have a definition // for `type` when `edition` === "bedrock" | "dungeons"}]
Output
"use strict";letuses=[{edition: "java",type: "texture_pack",assets: "foo",// OK, the error was expected},{edition: "java",type: "resource_pack",assets: "minecraft",},{edition: "bedrock",type: "resource_pack"// NOT OK,// -> Shouldn't be allowed as TextureUse doesn't have a definition // for `type` when `edition` === "bedrock" | "dungeons"}];
This runs into limitations of the current excess property detection logic. When the target is a union, TS needs to find which type in the union is the one that should be used to mark properties as excess. The part of the algorithm that normally works here is to find the union discriminant, which is almostedition here, but not quite, because there's more than one type with edition: "java". So EPC says "Well, there doesn't seem to be an obvious target type here".
When no such type can be found, the fallback behavior is that any property from any type in the union is valid, which is what's happening here.
Activity
Juknum commentedon Jan 13, 2023
On the community discord we found out a workaround :
Then trying to assign anything to
typewill throw an error letting only one possibility : do not declare it.RyanCavanaugh commentedon Jan 14, 2023
This runs into limitations of the current excess property detection logic. When the target is a union, TS needs to find which type in the union is the one that should be used to mark properties as excess. The part of the algorithm that normally works here is to find the union discriminant, which is almost
editionhere, but not quite, because there's more than one type withedition: "java". So EPC says "Well, there doesn't seem to be an obvious target type here".When no such type can be found, the fallback behavior is that any property from any type in the union is valid, which is what's happening here.
RyanCavanaugh commentedon Jan 14, 2023
If someone can concoct a PR that can fix this without perf downside we'd take it, otherwise this might just be a "wait for exact types" thing.
Juknum commentedon Jul 4, 2023
Seems to have been fixed in 5.1.3 & 5.2.0-dev.20230704 👀
jakebailey commentedon Jul 5, 2023
The fix bisects to #51884. Seems like that's intentional and good? I think the tests cover this?
jakebailey commentedon Jul 6, 2023
I'm gonna vote yes 😄
Juknum commentedon Jul 6, 2023
Oops yeah I wasn't sure if your answer was for me or somebody else ^^"