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
typeQ={a?: string;};functionqwop(t: 'a'|'nope'): Q{return{[t]: 666,// this compiles};}functionqwop2(t: 'a'): Q{return{[t]: 666,// this gives type error as expected};}typeQ2={[Kin'a'|'b']?: string;};functionqwop3(t: 'a'): Q2{return{[t]: 666,// this gives type error as expected};}functionqwop4(t: 'a'|'b'): Q2{return{[t]: 666,// this compiles, bug?};}typeQ3={a?: string;b?: string;};functionqwop5(t: 'a'|'b'): Q3{return{[t]: 666,// this compiles, bug?};}
π Actual behavior
Type check stops working when using a string union type as dynamic key to an object.
π Expected behavior
First I expected 'a' | 'nope' to not be a valid type for key of { a?: string } but I guess extraneous keys are just fine in a duck eat duck world.
What seems to be a bug to me is that the type declaration is ignored when the key type is a union - even when the full union is properly declared in the type. Ie. the type of properties 'a' | 'b' is clearly string and I expected a number to produce a type error.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
π Search Terms
"union type as dynamic object key"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=5.4.3#code/FAguE8AcFMAIEVYF5YG9i1gQwPwC5YBnUAJwEsA7AcwG5gBfOgMwFcKBjUMgewtgEcA7t0gAKUAQDkWSbAA+sSRRHRJASgKJ0mEtFAsSfbZlgBtUAF0CANlsAaWAHpHsUAAsyhWO24BbSGQANtCEGLCMDMCsHFy8AsKQAEziUjIaCGhhuvqGmSZmljb2Ti7unrBUZABuIa5QcNAkJNwk2F7QAB4wnNAAJmER9GD1CInIeWYA0rCUijLyigBGkhb4RKSUtAzMbJw8fEIiAMwpc+qaY8bZBkZhmOZWsLbWDs6uHl6VNV4QMLCNzVaWHaXWgPX6mEGUV2sQOCQALKdpLIFJJlul4JcsnobhN7oUnsU3mUvD5-EEQg5FiwqDgBnQhsBfnB4EdxsZcARiORqHRMIs1tzNgydjF9vERABWJHzVHozRsq443LGfGPZ6vUofbx+ALBQhUml0yEMoA
π» Code
π Actual behavior
Type check stops working when using a string union type as dynamic key to an object.
π Expected behavior
First I expected
'a' | 'nope'
to not be a valid type for key of{ a?: string }
but I guess extraneous keys are just fine in a duck eat duck world.What seems to be a bug to me is that the type declaration is ignored when the key type is a union - even when the full union is properly declared in the type. Ie. the type of properties
'a' | 'b'
is clearlystring
and I expected anumber
to produce a type error.Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: