-
Notifications
You must be signed in to change notification settings - Fork 13
Add an Iterator.from
TypeScript goody
#417
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
Conversation
@miorel I think the snapshots are failing because of the
|
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
78e3ada
to
0b65684
Compare
5d60da1
to
319cf8b
Compare
980c625
to
319cf8b
Compare
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
if (typeof iterator === "object" && Symbol.iterator in iterator) { | ||
return iterator as IterableIterator<T>; | ||
} else { | ||
return { | ||
...iterator, | ||
[Symbol.iterator]() { | ||
return this; | ||
}, | ||
} as IterableIterator<T>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the implementation of .toIterable
here? I'm hoping we can call it with a custom this
, similar to what we were discussing this weekend.
if (typeof iterator === "object" && Symbol.iterator in iterator) { | |
return iterator as IterableIterator<T>; | |
} else { | |
return { | |
...iterator, | |
[Symbol.iterator]() { | |
return this; | |
}, | |
} as IterableIterator<T>; | |
} | |
return iteratorPrototype.toIterable.call(iterator); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit confused did we want the custom annotation kind of like
declare global {
interface IteratorConstructor {
from<T>(
this: Iterator<T>, // this context
object: Iterator<T> | Iterable<T> | { next(): IteratorResult<T> },
): IterableIterator<T>;
}
const Iterator: IteratorConstructor;
}
Also while returning
return iteratorPrototype.toIterable.call(iterator);
it seems that I need to assert as IterableIterator<T>
because of the type error
Type 'IterableIterator<unknown>' is not assignable to type 'IterableIterator<T>'.
The types returned by 'next(...)' are incompatible between these types.
Type 'IteratorResult<unknown, any>' is not assignable to type 'IteratorResult<T, any>'.
Type 'IteratorYieldResult<unknown>' is not assignable to type 'IteratorResult<T, any>'.
Type 'IteratorYieldResult<unknown>' is not assignable to type 'IteratorYieldResult<T>'.
Type 'unknown' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.ts(2322)
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
if (typeof (object as Iterable<T>)[Symbol.iterator] === "function") { | ||
const iterable = object as Iterable<T>; | ||
const iterator = iterable[Symbol.iterator](); | ||
return iteratorPrototype.toIterable.call(iterator) as IterableIterator<T>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fun question for you: why do we have to do iteratorFactory.call(object)
and can't just do iteratorFactory()
?
if (typeof (object as Iterable<T>)[Symbol.iterator] === "function") { | |
const iterable = object as Iterable<T>; | |
const iterator = iterable[Symbol.iterator](); | |
return iteratorPrototype.toIterable.call(iterator) as IterableIterator<T>; | |
} | |
const iteratorFactory = (object as Iterable<T>)[Symbol.iterator]; | |
if (typeof iteratorFactory === "function") { | |
return toIterable.call(iteratorFactory.call(object)); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah this is because of the binding of this
right, and we need to bind object
to iteratorFactory
when invoking the function?
if (Object.prototype.isPrototypeOf.call(iteratorPrototype, object)) { | ||
return iteratorPrototype.toIterable.call( | ||
object as Iterator<T>, | ||
) as IterableIterator<T>; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (Object.prototype.isPrototypeOf.call(iteratorPrototype, object)) { | |
return iteratorPrototype.toIterable.call( | |
object as Iterator<T>, | |
) as IterableIterator<T>; | |
} | |
if (iteratorPrototype.isPrototypeOf(object)) { | |
return toIterable.call(object as Iterator<T>); | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.ts
Outdated
Show resolved
Hide resolved
workspaces/adventure-pack/goodies/typescript/Iterator.from/index.test.ts
Outdated
Show resolved
Hide resolved
c82b09f
to
7686304
Compare
based off of MDN
closes #343