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
typeMyKeyType='a'|'b'|'c';typeSubValuesType={values: string[];};typeMyContainerType={badValues: Partial<Record<MyKeyType,SubValuesType>>;};functionafunction(akey: MyKeyType){letkey=akey;//key = 'c'; // uncomment me and assign any of a, b or c and compilation failsconstnewData: 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.
The text was updated successfully, but these errors were encountered:
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.
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.
Bug Report
π Search Terms
typescript partial record key type checking failure
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=4.6.2#code/C4TwDgpgBAsiDSEQBVzQLxQOQEMtQB9sAjfIrAYywG4AoUSKAZQFdiA1HAGxYgGdUjTAG9aUKADduvPgC4ofYACcAlgDsA5gG0AunQC+demlggAwgHs1wHOohLBGKKPHEcAE049+8gAo4lYBVuAB4AJQgKCyV3ELhEFDQAGmY2LxlHAD5MgyMAMxY1CiCrKBwCopK1AAocAGskeXikRwBKFyguCGAoBpAoTHqkOnEAelG+gewqaihxqEKogFsliGsoVbK1dzK+PhUNNS3+izyylOIoaKgKLZ3lsBUuHCqoPNsuPjEbq0UoNQgAHcACIvHBNcxWGx2BwmERQNyeaQ+ZxQLR9HTyYTHYAAC3UGgAkgB1HDWeS6KD6KlUgxAA
π» Code
π 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.
The text was updated successfully, but these errors were encountered: