Closed panrafal closed 7 years ago
@panrafal please can you paste your full .babelrc
?
@phpnode it's in the project: https://github.com/panrafal/babel-typecheck-test/blob/master/.babelrc
Our original project is huge, but I've distilled the problems to these two test files which fail every time. I can run all typecheck's tests without problems but trying to compile these files in the same directory also fails.
Ok this is two separate issues:
1 - The import test doesn't work because we don't support default imports/exports for types. If you change the statement to
import type {wtf} from './wtf'
it works. I'm not sure whether flow supports this, I'll investigate before v4.
2 - The second example is actually failing because of a bug in babel-generator - if you remove the comment it works.
@phpnode Thanks for the quick reply.
1) It worked before and according to this blog post its supported in flow: http://flowtype.org/blog/2015/02/18/Import-Types.html
Another problem I have now, is that after using a named import, the type is checked by calling it, instead of using instanceof. Most of imported types are classes. You have an unreleased version that I believe should fix that. Is there anything holding you back from the release?
2) With the complexity of Babel ecosystem blaiming is hard :) This can be fixed by declaring babel-generator: '6.4.5' in package.json. there is an unmerged PR fixing this already.
@panrafal because we can't do whole program analysis (this was a planned feature of babel 6 that didn't make it to release) we have no way of telling what flavour of entity you're importing when you import type
. Therefore we treat it just like a type
you've declared. This is definitely not ideal.
For v4 I want to really focus on compatibility with flow and this is an example of something that is hard to do within the confines of a babel plugin as it stands. It's possible that in order to solve this, we have to stop being a babel plugin and run our own compilation passes.
@phpnode But if every typecheck would use instanceof, wouldn't that actually solve it? As long as you have babel-instanceof enabled, you don't have to know if it's a class, or a type declaration - you can just use instanceof.
As a workaround for now, I understand that I can do this kind of stuff (it compiles, looks like it should work):
// a.js
export default class Foo {}
export type FooClass = Foo
// b.js
import type {FooClass} from './a'
function foo(param: FooClass) {}
@panrafal in the past I did use that Symbol.hasInstance
approach but ran into issues with babel's plugin ordering, the new passPerPreset
option might fix that.
Hi, sorry for taking so long to respond to this, this project is now deprecated in favour of https://codemix.github.io/flow-runtime which aims for full compatibility with Flow.
babel-plugin-flow-runtime
does not have this particular bug because we can tell the difference between types and values - types are instanceof Type
.
Since upgrading babel to 6.5.x, I've found two issues with code which compiled without problems before.
Babel-cli is at version 6.5.1, babel-core at 6.5.2 Node at 5.4.1 (happens at 5.6.0 too)
Both examples fail when built using
babel test...js
. You can find the test project here: https://github.com/panrafal/babel-typecheck-testQuote after opening brace of object type
Results in:
Import type declaration
Results in: