Skip to content
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

Partial record key type checking failure #48136

Closed
AntonOfTheWoods opened this issue Mar 6, 2022 · 3 comments
Closed

Partial record key type checking failure #48136

AntonOfTheWoods opened this issue Mar 6, 2022 · 3 comments
Labels
Duplicate An existing issue was already created
Milestone

Comments

@AntonOfTheWoods
Copy link

AntonOfTheWoods commented Mar 6, 2022

Bug Report

πŸ”Ž Search Terms

typescript partial record key type checking failure

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about "Common "Bugs" That Aren't Bugs"

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.6.2#code/C4TwDgpgBAsiDSEQBVzQLxQOQEMtQB9sAjfIrAYywG4AoUSKAZQFdiA1HAGxYgGdUjTAG9aUKADduvPgC4ofYACcAlgDsA5gG0AunQC+demlggAwgHs1wHOohLBGKKPHEcAE049+8gAo4lYBVuAB4AJQgKCyV3ELhEFDQAGmY2LxlHAD5MgyMAMxY1CiCrKBwCopK1AAocAGskeXikRwBKFyguCGAoBpAoTHqkOnEAelG+gewqaihxqEKogFsliGsoVbK1dzK+PhUNNS3+izyylOIoaKgKLZ3lsBUuHCqoPNsuPjEbq0UoNQgAHcACIvHBNcxWGx2BwmERQNyeaQ+ZxQLR9HTyYTHYAAC3UGgAkgB1HDWeS6KD6KlUgxAA

πŸ’» Code

type MyKeyType = 'a' | 'b' | 'c';
type SubValuesType = {
  values: string[];
};

type MyContainerType = {
  badValues: Partial<Record<MyKeyType, SubValuesType>>;
};

function afunction(akey: MyKeyType){
  let key = akey;
  //key = 'c'; // uncomment me and assign any of a, b or c and compilation fails
  const newData: MyContainerType = { badValues: { [key]: { anythingIWant: [] } } };
}

πŸ™ Actual behavior

There is no type checking compilation error. I am able to assign an invalid type to newData

πŸ™‚ Expected behavior

Checking error. At the very least I would call this a MASSIVE "principle of least surprise" failure. If this is the expected behaviour, it is extremely unintuitive.

@RyanCavanaugh RyanCavanaugh added Experimentation Needed Someone needs to try this out to see what happens Suggestion An idea for TypeScript labels Mar 8, 2022
@RyanCavanaugh
Copy link
Member

Sort of a duplicate of #38663

I think we could get away with raising an error for t = { [k]: v } when k is a literal union type "s1" | "s2" | "s3" ... and v isn't assignable to the union t["s1"] | t["s2"] | t["s3"] | ... (intersection would be more sound, but impractical due to producing never a lot of the time). It'd be a breaking change but it's hard to see how that code wouldn't have an actual bug.

@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Mar 8, 2022
@AntonOfTheWoods
Copy link
Author

Sorry I missed the duplicate @RyanCavanaugh . Looking at the issue you refer to looks close enough to me. If the fix you think might be viable here would work there also, I'm definitely happy to close this.

@RyanCavanaugh
Copy link
Member

On second read, the issue I linked is definitely the same. I'll transfer the metadata

@RyanCavanaugh RyanCavanaugh added Duplicate An existing issue was already created and removed Suggestion An idea for TypeScript Experimentation Needed Someone needs to try this out to see what happens labels Mar 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants