Skip to content

Commit 2abd042

Browse files
committed
and I think we're good!
1 parent 62f9a32 commit 2abd042

2 files changed

Lines changed: 32 additions & 15 deletions

File tree

test/assoc.test.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const obj: BasicObj = { str: 'foo', num: 1 };
1313
//
1414

1515
// assoc(key)(__, obj)(val)
16+
expectError(assoc(__));
1617
expectType<BasicObj>(assoc('str')(__, obj)('bar'));
1718
expectType<Omit<BasicObj, 'str'> & Record<'str', number>>(assoc('str')(__, obj)(2));
1819
expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc('str')(__, {} as { other: string })('foo'));
@@ -61,7 +62,7 @@ expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc('str',
6162
//
6263

6364
// assoc(__, __, obj)(key)(val)
64-
expectError(assoc(__, __, obj)(__)('bar'));
65+
expectError(assoc(__, __, obj)(__));
6566
expectType<BasicObj>(assoc(__, __, obj)('str')('bar'));
6667
expectType<Omit<BasicObj, 'str'> & Record<'str', number>>(assoc(__, __, obj)('str')(2));
6768
expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc(__, __, {} as { other: string })('str')('bar'));
@@ -75,3 +76,23 @@ expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc(__, __,
7576
expectType<BasicObj>(assoc(__, __, obj)('str', 'bar'));
7677
expectType<Omit<BasicObj, 'str'> & Record<'str', number>>(assoc(__, __, obj)('str', 2));
7778
expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc(__, __, {} as { other: string })('str', 'bar'));
79+
80+
//
81+
// rest
82+
//
83+
84+
// assoc(__, val, obj)(prop)
85+
expectError(assoc(__,'bar', obj)(__));
86+
expectType<BasicObj>(assoc(__,'bar', obj)('str'));
87+
expectType<Omit<BasicObj, 'str'> & Record<'str', number>>(assoc(__, 2, obj)('str'));
88+
expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc(__, 'bar', {} as { other: string })('str'));
89+
90+
// assoc(key, __, obj)(__, val)
91+
expectType<BasicObj>(assoc('str', __, obj)('bar'));
92+
expectType<Omit<BasicObj, 'str'> & Record<'str', number>>(assoc('str', __, obj)(2));
93+
expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc('str', __, {} as { other: string })('bar'));
94+
95+
// assoc(key, val, obj)
96+
expectType<BasicObj>(assoc('str', 'bar', obj));
97+
expectType<Omit<BasicObj, 'str'> & Record<'str', number>>(assoc('str', 2, obj));
98+
expectType<Omit<{ other: string }, 'str'> & Record<'str', string>>(assoc('str', 'foo', {} as { other: string }));

types/assoc.d.ts

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Placeholder, AssocResults } from './util/tools';
22

33
// assoc(prop)
4-
export function assoc<K extends PropertyKey>(prop: K): {
4+
export function assoc<K extends PropertyKey>(prop: K extends Placeholder ? never : K): {
55
// assoc(prop)(val)
66
<T>(val: T): {
77
// assoc(prop)(val)(obj), when obj has key prop, tests if val is typeof obj[prop] for best return type
@@ -38,7 +38,7 @@ export function assoc<T>(__: Placeholder, val: T) : {
3838

3939
// assoc(prop, val)
4040
export function assoc<K extends PropertyKey, T>(prop: K extends Placeholder ? never : K, val: T): {
41-
// assoc(prop, val)(obj), when obj has key prop, tests if val is typeof obj[prop] for best return type
41+
// assoc(prop, val)(obj)
4242
<U>(obj: U): AssocResults<K, T, U>;
4343
};
4444

@@ -54,15 +54,11 @@ export function assoc<U>(__: Placeholder, __2: Placeholder, obj: U): {
5454
<K extends PropertyKey, T>(prop: K, val: T): AssocResults<K, T, U>;
5555
};
5656

57-
// assoc(prop, __, obj)(val), when prop is not keyof obj
58-
export function assoc<K extends PropertyKey, U>(prop: K extends Placeholder ? never : K, __: Placeholder, obj: U): <T>(val: T) => U & Record<K, T>;
59-
// assoc(prop, __, obj)(val), when K is keyof obj, tests if val is typeof obj[prop] for best return type
60-
export function assoc<K extends keyof U, U>(prop: K, __: Placeholder, obj: U): <T>(val: T) => T extends U[K] ? U : Omit<U, K> & Record<K, T>;
61-
// assoc(__, val, obj)(prop), this tests if prop is keyof obj and if val is typeof obj[prop] for best return type
62-
export function assoc<T, U>(__: Placeholder, val: T extends Placeholder ? never : T, obj: U): <K extends PropertyKey>(prop: K) => K extends keyof U ? T extends U[K] ? U : Omit<U, K> & Record<K, T> : U & Record<K, T>;
63-
// assoc(prop, val, obj) when prop is not keyof obj
64-
export function assoc<K extends PropertyKey, T, U>(prop: K extends Placeholder ? never : K, val: T extends Placeholder ? never : T, obj: U): U & Record<K, T>;
65-
// assoc(prop, val, obj) when prop is keyof obj and val is not same type
66-
export function assoc<K extends keyof U, T, U>(prop: K, val: T extends Placeholder ? never : T, obj: U): Omit<U, K> & Record<K, T>;
67-
// assoc(prop, val, obj) when prop is keyof obj and val is same type
68-
export function assoc<K extends keyof U, U>(prop: K, val: U[K], obj: U): U;
57+
// assoc(__, val, obj)(prop)
58+
export function assoc<T, U>(__: Placeholder, val: T, obj: U): <K extends PropertyKey>(prop: K extends Placeholder ? never : K) => AssocResults<K, T, U>;
59+
60+
// assoc(prop, __, obj)(val)
61+
export function assoc<K extends PropertyKey, U>(prop: K, __: Placeholder, obj: U): <T>(val: T) => AssocResults<K, T, U>;
62+
63+
// assoc(prop, val, obj)
64+
export function assoc<K extends PropertyKey, T, U>(prop: K, val: T, obj: U): AssocResults<K, T, U>;

0 commit comments

Comments
 (0)