Skip to content

Commit

Permalink
[Fix] Array types generated array methods (#10)
Browse files Browse the repository at this point in the history
* Fix a bug where arrays types generate array methods as type

* Bump version to 0.1.3
  • Loading branch information
BatuhanW authored Apr 20, 2021
1 parent 08c5df9 commit 4cd7fcb
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
.idea/**/gradle.xml
.idea/**/libraries

.idea

# Gradle and Maven with auto-import
# When using Gradle or Maven with auto-import, you should exclude module files,
# since they will be recreated, and may cause churn. Uncomment if using
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@batuhanw/haf",
"version": "0.1.2",
"version": "0.1.3",
"engines": {
"node": ">=10"
},
Expand Down
2 changes: 1 addition & 1 deletion spec/__type-snapshots__/types.spec.ts.snap.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"getName":"string","getAge":"number","getAppearance":"Appearance","getBirthMarks":"string[]","getEyeColor":"string","getHairColor":"HairColor","getOtherColors":"string[]","getPrimary":"string","getSecondary":"string | undefined","getNoseColor":"string | undefined","getFavoriteToys":"string[]","getHasPuppies":"boolean","getLuckNumbers":"number[]","getSterilizedAt":"string | undefined","getVaccines":"Vaccine[]","getVaccines[0]":"Vaccine","getError":"string | number | boolean | string[] | (() => string) | (() => string) | (() => string | undefined) | ((...items: string[]) => number) | { (...items: ConcatArray<string>[]): string[]; (...items: (string | ConcatArray<string>)[]): string[]; } | ((separator?: string | undefined) => string) | (() => string[]) | (() => string | undefined) | ((start?: number | undefined, end?: number | undefined) => string[]) | ((compareFn?: ((a: string, b: string) => number) | undefined) => string[]) | { (start: number, deleteCount?: number | undefined): string[]; (start: number, deleteCount: number, ...items: string[]): string[]; } | ((...items: string[]) => number) | ((searchElement: string, fromIndex?: number | undefined) => number) | ((searchElement: string, fromIndex?: number | undefined) => number) | { <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): boolean; } | ((predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any) => boolean) | ((callbackfn: (value: string, index: number, array: string[]) => void, thisArg?: any) => void) | (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | { <S extends string>(predicate: (value: string, index: number, array: string[]) => value is S, thisArg?: any): S[]; (predicate: (value: string, index: number, array: string[]) => unknown, thisArg?: any): string[]; } | { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string; <U>(callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } | { (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string): string; (callbackfn: (previousValue: string, currentValue: string, currentIndex: number, array: string[]) => string, initialValue: string): string; <U>(callbackfn: (previousValue: U, currentValue: string, currentIndex: number, array: string[]) => U, initialValue: U): U; } | { <S extends string>(predicate: (this: void, value: string, index: number, obj: string[]) => value is S, thisArg?: any): S | undefined; (predicate: (value: string, index: number, obj: string[]) => unknown, thisArg?: any): string | undefined; } | ((predicate: (value: string, index: number, obj: string[]) => unknown, thisArg?: any) => number) | ((value: string, start?: number | undefined, end?: number | undefined) => string[]) | ((target: number, start: number, end?: number | undefined) => string[]) | (() => IterableIterator<[number, string]>) | (() => IterableIterator<number>) | (() => IterableIterator<string>) | ((searchElement: string, fromIndex?: number | undefined) => boolean) | number[] | (() => string) | (() => string) | (() => number | undefined) | ((...items: number[]) => number) | { (...items: ConcatArray<number>[]): number[]; (...items: (number | ConcatArray<number>)[]): number[]; } | ((separator?: string | undefined) => string) | (() => number[]) | (() => number | undefined) | ((start?: number | undefined, end?: number | undefined) => number[]) | ((compareFn?: ((a: number, b: number) => number) | undefined) => number[]) | { (start: number, deleteCount?: number | undefined): number[]; (start: number, deleteCount: number, ...items: number[]): number[]; } | ((...items: number[]) => number) | ((searchElement: number, fromIndex?: number | undefined) => number) | ((searchElement: number, fromIndex?: number | undefined) => number) | { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): boolean; } | ((predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any) => boolean) | ((callbackfn: (value: number, index: number, array: number[]) => void, thisArg?: any) => void) | (<U>(callbackfn: (value: number, index: number, array: number[]) => U, thisArg?: any) => U[]) | { <S extends number>(predicate: (value: number, index: number, array: number[]) => value is S, thisArg?: any): S[]; (predicate: (value: number, index: number, array: number[]) => unknown, thisArg?: any): number[]; } | { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } | { (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number): number; (callbackfn: (previousValue: number, currentValue: number, currentIndex: number, array: number[]) => number, initialValue: number): number; <U>(callbackfn: (previousValue: U, currentValue: number, currentIndex: number, array: number[]) => U, initialValue: U): U; } | { <S extends number>(predicate: (this: void, value: number, index: number, obj: number[]) => value is S, thisArg?: any): S | undefined; (predicate: (value: number, index: number, obj: number[]) => unknown, thisArg?: any): number | undefined; } | ((predicate: (value: number, index: number, obj: number[]) => unknown, thisArg?: any) => number) | ((value: number, start?: number | undefined, end?: number | undefined) => number[]) | ((target: number, start: number, end?: number | undefined) => number[]) | (() => IterableIterator<[number, number]>) | (() => IterableIterator<number>) | (() => IterableIterator<number>) | ((searchElement: number, fromIndex?: number | undefined) => boolean) | Vaccine[] | (() => string) | (() => string) | (() => Vaccine | undefined) | ((...items: Vaccine[]) => number) | { (...items: ConcatArray<Vaccine>[]): Vaccine[]; (...items: (Vaccine | ConcatArray<Vaccine>)[]): Vaccine[]; } | ((separator?: string | undefined) => string) | (() => Vaccine[]) | (() => Vaccine | undefined) | ((start?: number | undefined, end?: number | undefined) => Vaccine[]) | ((compareFn?: ((a: Vaccine, b: Vaccine) => number) | undefined) => Vaccine[]) | { (start: number, deleteCount?: number | undefined): Vaccine[]; (start: number, deleteCount: number, ...items: Vaccine[]): Vaccine[]; } | ((...items: Vaccine[]) => number) | ((searchElement: Vaccine, fromIndex?: number | undefined) => number) | ((searchElement: Vaccine, fromIndex?: number | undefined) => number) | { <S extends Vaccine>(predicate: (value: Vaccine, index: number, array: Vaccine[]) => value is S, thisArg?: any): this is S[]; (predicate: (value: Vaccine, index: number, array: Vaccine[]) => unknown, thisArg?: any): boolean; } | ((predicate: (value: Vaccine, index: number, array: Vaccine[]) => unknown, thisArg?: any) => boolean) | ((callbackfn: (value: Vaccine, index: number, array: Vaccine[]) => void, thisArg?: any) => void) | (<U>(callbackfn: (value: Vaccine, index: number, array: Vaccine[]) => U, thisArg?: any) => U[]) | { <S extends Vaccine>(predicate: (value: Vaccine, index: number, array: Vaccine[]) => value is S, thisArg?: any): S[]; (predicate: (value: Vaccine, index: number, array: Vaccine[]) => unknown, thisArg?: any): Vaccine[]; } | { (callbackfn: (previousValue: Vaccine, currentValue: Vaccine, currentIndex: number, array: Vaccine[]) => Vaccine): Vaccine; (callbackfn: (previousValue: Vaccine, currentValue: Vaccine, currentIndex: number, array: Vaccine[]) => Vaccine, initialValue: Vaccine): Vaccine; <U>(callbackfn: (previousValue: U, currentValue: Vaccine, currentIndex: number, array: Vaccine[]) => U, initialValue: U): U; } | { (callbackfn: (previousValue: Vaccine, currentValue: Vaccine, currentIndex: number, array: Vaccine[]) => Vaccine): Vaccine; (callbackfn: (previousValue: Vaccine, currentValue: Vaccine, currentIndex: number, array: Vaccine[]) => Vaccine, initialValue: Vaccine): Vaccine; <U>(callbackfn: (previousValue: U, currentValue: Vaccine, currentIndex: number, array: Vaccine[]) => U, initialValue: U): U; } | { <S extends Vaccine>(predicate: (this: void, value: Vaccine, index: number, obj: Vaccine[]) => value is S, thisArg?: any): S | undefined; (predicate: (value: Vaccine, index: number, obj: Vaccine[]) => unknown, thisArg?: any): Vaccine | undefined; } | ((predicate: (value: Vaccine, index: number, obj: Vaccine[]) => unknown, thisArg?: any) => number) | ((value: Vaccine, start?: number | undefined, end?: number | undefined) => Vaccine[]) | ((target: number, start: number, end?: number | undefined) => Vaccine[]) | (() => IterableIterator<[number, Vaccine]>) | (() => IterableIterator<number>) | (() => IterableIterator<Vaccine>) | ((searchElement: Vaccine, fromIndex?: number | undefined) => boolean) | Appearance | HairColor | undefined"}
{"getName":"string","getAge":"number","getAppearance":"Appearance","getBirthMarks":"string[]","getEyeColor":"string","getHairColor":"HairColor","getOtherColors":"string[]","getPrimary":"string","getSecondary":"string | undefined","getNoseColor":"string | undefined","getFavoriteToys":"string[]","getHasPuppies":"boolean","getLuckyNumbers":"number[]","getSterilizedAt":"string | undefined","getVaccines":"Vaccine[]","getVaccines[0]":"Vaccine","getError":"string | number | boolean | string[] | number[] | Vaccine[] | Appearance | HairColor | undefined","getArrayError":"string | number | boolean | string[] | number[] | Vaccine[] | Appearance | HairColor | undefined","getNestedArrayError":"string | number | boolean | string[] | number[] | Vaccine[] | Appearance | HairColor | undefined"}
10 changes: 9 additions & 1 deletion spec/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('Type tests', () => {
// $ExpectTypeSnapshot getHasPuppies
haf.get('hasPuppies');

// $ExpectTypeSnapshot getLuckNumbers
// $ExpectTypeSnapshot getLuckyNumbers
haf.get('luckyNumbers');

// $ExpectTypeSnapshot getSterilizedAt
Expand All @@ -78,5 +78,13 @@ describe('Type tests', () => {
// @ts-expect-error: Getting a key that doesn't exist
// $ExpectTypeSnapshot getError
haf.get('non-existent');

// @ts-expect-error: Arrays shouldn't generate types of array methods
// $ExpectTypeSnapshot getArrayError
haf.get('luckyNumbers.concat');

// @ts-expect-error: Deeply nested arrays shouldn't generate types of array methods
// $ExpectTypeSnapshot getNestedArrayError
haf.get('appearance.hairColor.otherColors.concat');
});
});
10 changes: 5 additions & 5 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ export type FlattenedWithDotNotation<Schema, Prefix = null> =
/* then, for each sub-object, recurse */
IntersectValuesOf<
{
[K in string & keyof Schema as AddPrefix<K, Prefix>]: Schema[K] extends Record<
string,
// eslint-disable-next-line
any
>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[K in string & keyof Schema as AddPrefix<K, Prefix>]: Schema[K] extends Array<any>
? never
// eslint-disable-next-line @typescript-eslint/ban-types
: Schema[K] extends object
? FlattenedWithDotNotation<Schema[K], AddPrefix<K, Prefix>>
: never;
}
Expand Down

0 comments on commit 4cd7fcb

Please sign in to comment.