-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Using IteratorResult errors unless you build with --ES6 #3445
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
The temporary solution was to add a /typings/fixes/fixes.d.ts interface IteratorResult<T> {
done: boolean;
value?: T
} |
You can use the interface wherever you want when it's defined, hence your workaround. With --target ES6 you get the lib.d.ts that contains the definition for IteratorResult, when you don't, you get the ES5 lib which doesn't say anything about Iterators. We have another issue somewhere with some discussion of breaking up the association between ES target and the lib.d.ts version. |
As @danquirk mentioned, the definition for the interface is in |
it seems like, for the majority of interfaces at least, it shouldn't matter that I'm targeting ES6 or ES5. I'm just saying "I expect this contract for this object". I understand that certain declarations won't be in place, but interfaces seem legit to me. |
I agree interfaces are fine. it is just untangling what is fine and what is not is a bit.. manual. should |
It matters in the sense that your completion lists are now filled with a mix of things that will and won't work, and the compiler is not giving an error for things that don't actually exist where you'll be running your code. Yes they're just types so there's no missing runtime code but if you're writing a bunch of TS against the Iterator concept but then executing on ES5 you're probably not getting the experience you want unless you're aware of the distinction. By default we're trying to lead people down the happy path where the compiler is aware of your target runtime and doesn't provide things which don't exist there. Obviously some folks like yourself are targeting a variety of runtimes (with and without a variety of polyfills) which is why we may need to decouple the |
But interfaces with simple types do exist... interface IteratorResult<T> {
value?:T;
done:boolean
} And |
But all the things you are likely to want to do with an Iterator require ES6 unless you have a post-build step that transpiles for you. Since that post-build step doesn't exist by default and we can't tell whether it's there we're trying not to lead someone down a path where they use a bunch of Iterator based APIs when targeting ES5. |
Any interface can be implemented and returned in plain es5. As I'm sure you're totally aware, this is one of the principles on which transpilers are built. If I want to return a type that doesn't exist in my target, then the onus is on me to polyfill that type. tsc could warn me, maybe, but their is no reason it should error. All that does is cause me to add the interface myself, or worse, return type |
Incidentally, I've just added the interface myself to each file I need it in. So I've worked around this sort of unfriendly feature. I haven't closed this only because I thought you might want to track the concern. |
We are looking into allowing |
This doesn't make any sense, it's just an interface. I should be able to use it wherever I want.
tsc Test.ts --modules "commonjs"
and I get
error TS2304: Cannot find name 'IteratorResult'.
It will build without errors if I use
--target es6
, but I'm trying to target multiple things, CJS, AMD, ES6 and ES5 globals.The text was updated successfully, but these errors were encountered: