Skip to content
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

[BUGFIX release] Correct types for Ember Arrays #20256

Merged
merged 2 commits into from
Nov 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions packages/@ember/-internals/glimmer/lib/utils/iterator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type { Option } from '@glimmer/interfaces';
import type { IteratorDelegate } from '@glimmer/reference';
import { consumeTag, isTracking, tagFor } from '@glimmer/validator';
import { EachInWrapper } from '../helpers/each-in';
import type { NativeArray } from '@ember/array';

export default function toIterator(iterable: unknown): Option<IteratorDelegate> {
if (iterable instanceof EachInWrapper) {
Expand Down Expand Up @@ -100,11 +101,11 @@ class ArrayIterator extends BoundedIterator {
}

class EmberArrayIterator extends BoundedIterator {
static from(iterable: EmberArray<unknown>) {
static from(iterable: EmberArray<unknown> | NativeArray<unknown>) {
return iterable.length > 0 ? new this(iterable) : null;
}

constructor(private array: EmberArray<unknown>) {
constructor(private array: EmberArray<unknown> | NativeArray<unknown>) {
super(array.length);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/@ember/-internals/utils/lib/ember-array.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type EmberArray from '@ember/array';
import type { EmberArrayLike } from '@ember/array';
import { _WeakSet } from '@glimmer/util';

const EMBER_ARRAYS = new _WeakSet();
Expand All @@ -7,6 +7,6 @@ export function setEmberArray(obj: object) {
EMBER_ARRAYS.add(obj);
}

export function isEmberArray(obj: unknown): obj is EmberArray<unknown> {
export function isEmberArray(obj: unknown): obj is EmberArrayLike<unknown> {
return EMBER_ARRAYS.has(obj as object);
}
20 changes: 17 additions & 3 deletions packages/@ember/-internals/utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
export type AnyFn = (...args: any[]) => any;

export type MethodNamesOf<T> = {
[K in keyof T]: T[K] extends AnyFn ? K : never;
}[keyof T];
// The formatting here is designed to help make this type actually be
// comprehensible to mortals, including the mortals who came up with it.
// prettier-ignore
export type MethodsOf<T> = {
// This `keyof` check is the thing which gives us *only* these keys, and no
// `foo: never` appears in the final type.
[K in keyof T as T[K] extends AnyFn ? K : never]:
// While this makes sure the resolved type only has `AnyFn` in it, so that
// the resulting type is known to be only function types.
T[K] extends AnyFn ? T[K] : never;
};

export type MethodNamesOf<T> = keyof MethodsOf<T>;

export type MethodParams<T, M extends MethodNamesOf<T>> = Parameters<MethodsOf<T>[M]>;

export type MethodReturns<T, M extends MethodNamesOf<T>> = ReturnType<MethodsOf<T>[M]>;

export type OmitFirst<F> = F extends [any, ...infer R] ? R : [];
Loading