-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
narrow from 'any' in most situations #10319
Conversation
Hi @yortus, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! TTYL, MSBOT; |
Can I get some help with the tests? There's one failing conformance test (tests/cases/conformance/expressions/typeGuards/typeGuardsWithInstanceOfByConstructorSignature.ts). How do I fix that? Is that a case accepting new baselines? Also should I add any new tests for code coverage with this change? |
Hey @yortus looks like the codebase isn't even compiling according to the CI. I wonder what that's about. Make sure you're up to date with As for the tests, yes, if the baselines look acceptable (some files may be removed, others may be created), it's a matter of running I recommend a diffing tool that does a good job with folder diffs. Beyond Compare is our tool of choice, but WinMerge has also worked pretty great. You should also add tests for this change. Specifically, you should add tests that actually demonstrate
I'd put the tests in either I'd then name them something like
declare var x: any;
if (x instanceof Function) {
x();
x(1, 2, 3);
x("hello!");
x.prop;
}
if (x instanceof Object) {
x.method();
x();
} Of course, if you think other tests would be appropriate, it might be a decent idea to add some (e.g. one of the original complaints was that you couldn't narrow from a |
Thanks @DanielRosenwasser, I'll work on the tests. The CI failure is strange. My branch is up-to-date with the latest
I haven't touched that file, but I can see it hasn't been updated along with the other But I shouldn't be changing that in this PR. Any idea what's happening? |
This is potentially because we rely on a nightly of TSLint, which relies on a nightly from us. Though I'm not sure why my PR didn't have the same issue. In any case, check out #10323. |
I merged in the change, you can feel free to sync up with master now. |
instanceof and user-defined typeguards narrow from 'any' unless the narrowed-to type is exactly 'Object' or 'Function'. This is a breaking change.
4837be0
to
59c09d9
Compare
@DanielRosenwasser I've added the tests, so this PR should be ready now. Now there's another strange build error, but only for node 0.10. From the log:
Any insights? |
Not sure why that's happening; seems totally unrelated. We might need to contact Travis CI support. |
I'm just gonna close and re-open this PR to re-trigger the CI build, to rule out some intermittent factor. |
Hi @yortus, I'm your friendly neighborhood Microsoft Pull Request Bot (You can call me MSBOT). Thanks for your contribution! TTYL, MSBOT; |
Nope, same failure on node 0.10... |
The Node I've set up the CI on my fork of this PR which succeeds when all packages are installed. The remedy here seems to be to clear the cache which someone on the TypeScript team needs to do. |
Cleared the cache and restarted the build. Fingers crossed! |
Seems to have failed again 😡.
After that I guess the commits can just be removed using Alternatively just create a new PR and maybe this bad dream will all go away. |
I'll try opening a new PR. |
New PR at #10334 builds successfully. |
Sorry about whatever that was. 😕 as well here |
This PR implements narrowing from
any
as described in #9999 (comment).NB: this is a breaking change.
Fixes #9999, #10000, #8677
Checklist:
instanceof
or a user-defined type predicate will narrow unless the narrowed-to type is exactlyFunction
or exactlyObject
(in which case the expression remains of typeany
)jake runtests
locally