-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type () => T
accepts a function that returns an object with properties not inside T
.
#54661
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
Comments
Types are not exact, excess property checks are not comprehensive, and the object you’ve returned is assignable to |
Ultimately a duplicate of #241 I think |
Thanks for clarifying. It's unfortunate that this situation has not been resolved yet. |
Well, it’s not really a bug. The excess property check is considered a linter feature more than a type system feature so you shouldn’t rely on it to enforce runtime invariants. It’s only designed to catch cases where a fresh object literal is directly assigned to a type, which isn’t what’s happening here. Instead, you have an unannotated arrow function whose return type is first inferred, and then TS asks whether If you really need this check here, you can rewrite the code as const func = (): Test => ({ … }); Now you’re directly returning a fresh object literal as a |
@fatcerberus Makes sense, though I think a tsconfig option to strictly check function return types might be useful. I'll leave that up to discussion. |
Any stricter behavior here more or less makes this a duplicate of #12936, as otherwise the proposed behavior makes |
I have just encountered the same situation, and I think it's in fact the issue to be considered. The reason I think so can be illustrated with this simple example:
My expectations were that object constant and the function return types would undergo the same level of scrutiny, but they do not. I kinda understand what happens with the return type widening, but I still find it an issue. More specifically, I expect the function type to save me from one of the most common code problems I have: the typos and the similar mistakes. Now, consider this example:
Here, I have very obviously missed the case for the last prop. This will pass the typecheck and introduce the unexpected runtime situation. Now, I can elaborate on the two cases above and show how this behavior also introduces inconcistency not between objects and functions, but between two different ways to type-annotate the same function:
|
Bug Report
🔎 Search Terms
🕗 Version & Regression Information
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The compiler does not throw any errors, allowing the function to return an object with properties that are not present in interface
Test
.🙂 Expected behavior
The compiler should throw an error since property
d
does not exist on interfaceTest
.The text was updated successfully, but these errors were encountered: