-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
object literal property check inconsistent #16284
Comments
The first one is the intended design. TypeScript type system is structural, so it is OK to assign a value with more properties to one that expects less. think of The second issue is tracked by #12745. |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.3
Code
Expected behavior:
There should be a way to make both marks [1] and [2] error, as [0] does.
Actual behavior:
[1] and [2] don't error - and there is no way to structure the code to enforce stricter checks like this.
The behavior in [0] is very fragile, and can lead one to a false sense of security, especially in cases like [2] (where 'b' is present but is of the wrong type). It would be incredibly useful to require the type to be matched exactly.
Motivation:
I think it would be useful to describe the problem I'm trying to solve rather than just proposing a solution.
I am trying to add a layer of type-safety to my db queries. As an example, mongdb has an
_id
field that is of typeObjectID
. It's common to get such an id as part of an http request, in which case it is astring
, and forget to convert it into anObjectID
before using it in a query (in which case the query returns nothing).I thought I'd create a union type of possible queries that we might send to a particular collection:
but, this doesn't provide the desired type safety as explained above, and it seems this is not something that TypeScript can help with.
The text was updated successfully, but these errors were encountered: