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
//@ts-check/** * @typedef {Object.<string, *>} Persona * @property {string} name * @property {number} age *//** * Greets a person. * * @param {Persona} person - The person to greet. * @returns {string} A greeting message. */functiongreeta(person){return`Hello, ${person.name}! You are ${person.age} years old.`;}//Currently all of the above are valid code://EXPECTED: Should be invalid code when passing an occupation in an anonymous object to the functiongreeta({name:'Joe',age:25,occupation:'miner'})//EXPECTED: Should be valid code even though occupation is not part of the interface because it is defined in an object firstletjoea={name:'Joe',age:25,occupation:'miner'}greeta(joea);//EXPECTED: Both should be invalid code because age value is wrong typegreeta({name:'Lynnettte',age:'X',occupation:'miner'})letlynna={name:'Lynnette',age:'X',occupation:'miner'}greeta(lynna);
π Actual behavior
No errors appear in the TypeScript checker/compiler because it appears the Object.<string, *> declaration overrides the property declarations and allows any types for those named properties.
3 of the 4 calls should produce a type error if we are following the TypeScript convention that would apply to what I suppose would be the equivalent TypeScript type:
//EXPECTED: Should be invalid code when passing an occupation in an anonymous object to the functiongreeta({name:'Joe',age:25,occupation:'miner'})//EXPECTED: Should be valid code even though occupation is not part of the interface because it is defined in an object firstletjoea={name:'Joe',age:25,occupation:'miner'}greeta(joea);//EXPECTED: Both should be invalid code because age value is wrong typegreeta({name:'Lynnettte',age:'X',occupation:'miner'})letlynna={name:'Lynnette',age:'X',occupation:'miner'}greeta(lynna);
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
spillz
changed the title
Curious type checking behavior of Object.<string, *> typedefs with named properties in JSDoc. Is it a bug?
Curious type checking behavior of Object.<string, *> typedefs with named properties in JSDoc (for TS checks on JS code). Is it a bug?
Sep 7, 2023
I believe this typedef is more or less equivalent to Record<string, any> & { name: string, age: number } which isn't well-behaved even in native TypeScript. See #17867
/** * @typedef {Object} Persona * @property {string} name * @property {number} age */
will actually reproduce the correct behavior I was looking for, although it's unituitive that the absence of the <string, *> would do that. But what I'd also like is something that can reproduce the TypeScript that would allow the calls in the original JS code with occupation to pass type checks but flag the mis-typed age calls:
π Search Terms
Object.<string, *> JSDoc typedefs @Property
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?ts=4.2.3&filetype=js#code/PTACBcGcFoGMAsCmsDWAoYAqTaAEncIBPAB0QBNEAzXAbwHkAjAK2XADoAeScAJwEsAdgHMANPgB8AX1wAFRL0gB7QQEM8BUCV5KyvcETo8BImWoC2iDYW26FBuoICu5xgpmrhV-MDQZs1gDivIiIULiquHrKguzW1lqqvKrmdPKKKqoy0Sq40LgAKkhRCjG44Eq4wiFhcfiEIeBOvIKQRnxCwjIAglU14J24lpCQnoh1mL5UToKwA7nVoeCqABQ5ggCUdHi4jc2CuAAGABKIADZnSuIAJLTr7BaIUgCEuACaSk4RIbi392MyIiIJJtJRncjsQ4AbjQUj8GGAAGFmiFBOAzoZVBdcEoaOBiqpGEoAG6Ib5k4lY-jkXCwJSUABcfhAAFEABqyFmIgosgAiDNwAGV4J9wbg3LghJSztTafSyQB3JAHEiqEaDVQHJSwWBOVXzA5CCIHTUqIjmT6glhscqVfFk6azA1oRZhVa0R4MgDkACklIgveIxgyAEwAVnE2t1+v4Km95iECi9Ug2CPZnO5fIFwtFNIl0tldMouEQpIO+M+wngOJ1etUBslbUESnAUSSrdx5WKQnACioqlgZLcsFUTkgZP4rf4bUoVETNKNmpx1rmuDninAaDOYVwzH9kQAvI4UohvX6A0GvKGIzXo-XY4J44neMmXf1VnvgRsYWmOVyefyuAAEIttWkAik4YoSlKVI0kWQ7IKO44RF4uDSk4E5tAqOgiOUpBWK6ywrB6J7egAMkQgiCGE4C9oGKGnl6bL0VGdYGk+1EvimW47hiVGHseljkZR1G0ReDHesxka1jGcZegmnGvoRqx8Wo35oEAA
π» Code
π Actual behavior
No errors appear in the TypeScript checker/compiler because it appears the Object.<string, *> declaration overrides the property declarations and allows any types for those named properties.
3 of the 4 calls should produce a type error if we are following the TypeScript convention that would apply to what I suppose would be the equivalent TypeScript type:
π Expected behavior
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: