Skip to content

Partial record key type checking failureΒ #48136

Closed
@AntonOfTheWoods

Description

@AntonOfTheWoods

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions