-
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
Two types should only be compatible if their members are compatible - even if all these members are optional #12904
Comments
My understanding of trying to do this myself is that since all properties in |
that would make sense, but I don't think is the case since the following code does not compile, which is expected behaviour: let w1: MongoFilter<Person> = { wrong: 1 }; In this case the |
Note that this, on the other hand, compiles just fine: const x = { wrong: 1 };
let w1: MongoFilter<Person> = x; |
wow... so clearly there is a disconnect somewhere. My conclusion is that either type s = { };
let t: s = { wrong: 1 } How does it make sense that it compiles given the example above? Is it by design? |
Thanks for this link. Fantastic FAQ, I've read through the whole thing.
However, FAQ does not really provide the reason WHY it was decided to do it this way. I also didn't find any discussion about this. In my opinion The bottom line is that an object with the interface Person {
id?: number;
name?: string;
age?: number;
}
let wrongObject = { wrong: 1 };
let p: Person = wrongObject; // should not compile This supports the statement of what is structural typing, which is used by TypeScript (also from FAQ):
Changing the title now that I phrased the issue better. |
Glad to see I'm not the first one to see an issue with this! So to summarize: #12501 closes #1809, where @sccolbert said:
Correct me if I'm wrong - that's not the same as my issue. I could not understand, will this PR address this issue? #9841 and #12914 indeed discusses this issue, but is labeled with "Working as intended"? However, it mentions #3842, which I believe was intended to solve this issue and had a PR, but was never merged. Side note: the relevance of this issue will only increase now, that |
Excess property checks would have caught that if it operated on unions. tracked by #12745 |
Yep, i think this behaviour is very strange: const itFails: Partial<{ foo: number }> = { bar: 42 }; // TS2322:Type '{ bar: number; }' is not assignable
const ext = {bar: 42};
const butItWorks: Partial<{ foo: number }> = ext; // OK |
Fixed by #16047 |
I wanted to see if I can update definitions a MongoDB filter object, which utilizes these expressions. Below is just a start, but I already see an issue when the code compiles when it should not. This issue possibly has similar underlying issue as #12769
TypeScript Version: 2.1.4
Code
Expected behavior:
Code below should not compile
Actual behavior:
But it does
The text was updated successfully, but these errors were encountered: