-
Notifications
You must be signed in to change notification settings - Fork 12.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
No error is thrown when two types differ by one or more properties #56740
Comments
This is working as intended, see also: https://github.com/microsoft/TypeScript/wiki/FAQ#what-is-structural-typing You would need #12936 for this. |
To exclude specific properties you can do this: interface CleanedUser {
id: number
email: string
password?: never
} |
@MartinJohns Re: Confused emoji - optional properties typed as |
Confused is the wrong emoji, but I didn't want to give the thumbs down smiley. I really don't like this hacky workaround. Most of you wrote is correct, except the "the effect is to prevent the object from having that property at all". A property typed This code works perfectly fine and logs interface CleanedUser {
id: number
email: string
password?: never
}
const f: CleanedUser = {
id: 1,
email: 'foo',
get password(): never { throw new Error() }
}
console.log('password' in f) |
That's fair; I'd argue that still matches intent in this case since the effect is to prevent reading a password off of a "cleaned" user, but I can see that being problematic in other scenarios. The alternate version is Ultimately I agree that #12936 would be the non-hacky fix, but realistically it'll probably never happen. |
Note that any property could throw upon accessing, yet generally one interprets
I'd generally stop at the top bullet when discussing this unless unless such secondary and tertiary effects become important. I wouldn't call this a "hack", unless I wanted to reinterpret anything that neglects "this might throw" as a hack as well. |
@jcalz Bullet point 3 becomes somewhat more reasonable when you consider that the compiler won't stop you from accessing |
This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
error,thrown,types differ,one or more properties
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgKoGdrIN4ChnLAAmAXMiAK4C2ARtPshFXMADZnphSgDmDADnHToA7gHsopZJ24g+AX1y5QkWIhQBhVhDggIRDFjwFiZSrXoEmLdtK69ci3AjEhOyCpihlDUZAF4cBlNkAEYAGgZrNjIAcljIgkFhcUk42MclFzcwZARtXX1fMi0dPQMvAI8vJSA
π» Code
π Actual behavior
No error received when i assign User to CleanedUser
π Expected behavior
I would like to see an error that tells me that the CleanUser and User type are not equals because "password" or any additional properties are not allowed to the CleanedUser type.
Additional information about the issue
Is there a way to configure that behavior inside the tsconfig file?
The text was updated successfully, but these errors were encountered: