-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type inference broken in JS files - Behavior differs from typescript #30009
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
There are several intentional differences between JS and TS files as far as typechecking goes; this is one of them - you can "tack on" new properties to an object while in the same scope it was declared, i.e. this is still an error: // @ts-check
/** @returns {{prop: string}} */
function buildParams() {
const params = {
prop: `val`,
};
return params;
}
const p = buildParams();
// Error
p.filter = { mutation: 1 }; |
That's really confusing to be honest. Could you explain why this is Kind ask: At-least give us some sort of option/workaround to enable stricter js checking which is functionally equivalent to TS. |
There are several intentional differences between JS and TS files as far as typechecking goes; this is one of them - you can "tack on" new properties to an object. This was based on feedback of trying out the ts-check mode on many JS codebases.
Certainly open to a new suggestion on that. |
It seems like there might be two different fundamental use-cases in
the @ts-check world: 1) A stop-gap for people who want to migrate to
typescript but need to do it incrementally and 2) An alternative way to get
type checking using vanilla JS. The decision to diverge in behavior
from @ts-check and typescript makes sense when solving for use case #1.
Would you say this is an accurate assessment? Is there a long-term vision
that would support use case #2?
…On Fri, Mar 1, 2019 at 10:29 AM Ryan Cavanaugh ***@***.***> wrote:
Could you explain why this is working as intended
There are several *intentional* differences between JS and TS files as
far as typechecking goes; this is one of them - you can "tack on" new
properties to an object. This was based on feedback of trying out the
ts-check mode on many JS codebases.
At-least give us some sort of option/workaround to enable stricter js
checking which is functionally equivalent to TS.
Certainly open to a new suggestion on that.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#30009 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAh_UmyYp1G9b3v5Zd2MZvuxaMfxIM1Fks5vSXGBgaJpZM4bGVb3>
.
|
This issue has been marked 'Working as Intended' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Really typescript-bot? |
@RyanCavanaugh Should I create a separate issue to open up a discussion about the goals of typescript vs @ts-check? My concern is that if @ts-check is some weird subset of / divergent from typescript, it will not be usable as a replacement and will in fact result in a lot of unexpected behavior thus eliminating the utility of it. |
I have to document this anyway so I'll start here - here are the differences in typechecking between JS and TS files:
|
So yeah we can take a suggestion on something that e.g. turns off all of that behavior, or maybe just the certain aspects that you might find objectionable. That said, all of these were added on purpose based on user feedback and investigation of the ergonomics of writing passing code without the ability to write type assertions, type arguments, or type annotations, so it's worth thinking hard about what the exact trade-off being made is. "different inputs produce different behavior" is not a prima facie justification - some longer description of what you'd want from this mode would be useful, since simply renaming JS to TS is also a way to get the TS behavior. |
TypeScript Version: Version 3.3.3, Version 3.4.0-dev.20190220
Search Terms:
Object mutation, Inferred type, JSDoc
Code
Expected behavior:
Should error on object mutation and/or return type not matching
Actual behavior:
No errors
Example of bug
Proof that type checking works in general for js files
Same logic in typescript
The text was updated successfully, but these errors were encountered: