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
When using a union type that includes an extended interface with required properties, TypeScript allows an object to be assigned to the union type even if it has some but not all of the properties of one of the union members. This leads to a runtime error when accessing missing properties.
interfaceName{name: string;}interfacePersonWithBirthextendsName{dateOfBirth: Date;placeOfBirth: string;}typePerson=Name|PersonWithBirth;constaviv: Person={name: 'Aviv',placeOfBirth: 'TLV'};// No error herecreateUser(aviv);functioncreateUser(person: Person){if('placeOfBirth'inperson){person.dateOfBirth.getDay();// This will cause a runtime error}}
🙁 Actual behavior
TypeScript does not throw an error at the assignment of aviv, leading to a possible runtime error when accessing dateOfBirth.
🙂 Expected behavior
TypeScript should throw a type error at the assignment of aviv because it includes placeOfBirth, which is only defined in PersonWithBirth, but it is missing dateOfBirth, which is required by PersonWithBirth.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
🔎 Search Terms
When using a union type that includes an extended interface with required properties, TypeScript allows an object to be assigned to the union type even if it has some but not all of the properties of one of the union members. This leads to a runtime error when accessing missing properties.
🕗 Version & Regression Information
v5.7.3
⏯ Playground Link
https://www.typescriptlang.org/es/play/?ssl=21&ssc=1&pln=1&pc=1#code/JYOwLgpgTgZghgYwgAgHJwLYoN4ChnIiYQBcyAzmFKAOYDcuAvrrqJLIigArTkD2IAOrAwACwBCwKGOQQAHpBAATcmmLI8BJXEgB5GJOmiyAER0QGBAA4AbTvsNiylaiHpMWYAJ5XuvAcgAvGpYyAA+yDxQ-EIiElJiDLgIApTIcABuwBlkUTFBGoTEZADkAIJZGSUANMi29gYJxsglACoAMgBqJciMdMgA9ANofLJQUHxQyKLQECwIUBDmAKrk0AAUmdkAlEkwAK4gCGDAAQtLkKsbvtECuf4g2xr4yMAwyOsl9UgOTT2gdQeT00BEBtxAADptHpGkYITQIGAzF51rtBsNWqJgKoAO7AGw2ZAIOD7NbpZBQQ4nULQCZQF7MZhAA
💻 Code
🙁 Actual behavior
TypeScript does not throw an error at the assignment of aviv, leading to a possible runtime error when accessing dateOfBirth.
🙂 Expected behavior
TypeScript should throw a type error at the assignment of aviv because it includes placeOfBirth, which is only defined in PersonWithBirth, but it is missing dateOfBirth, which is required by PersonWithBirth.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: