Skip to content

Commit

Permalink
Adds workaround example for generics
Browse files Browse the repository at this point in the history
  • Loading branch information
ctrlplusb committed Nov 12, 2019
1 parent d4e8c72 commit 52c396b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type KeysOfType<A extends object, B> = {
* https://stackoverflow.com/questions/56422807/narrowing-a-type-to-its-properties-that-are-index-signatures/56423972#56423972
*/
type IndexSignatureKeysOfType<A extends Object> = {
[K in keyof A]: A[K] extends ({ [key: string]: any } | { [key: number]: any })
[K in keyof A]: A[K] extends { [key: string]: any } | { [key: number]: any }
? string extends keyof A[K]
? K
: number extends keyof A[K]
Expand Down
26 changes: 19 additions & 7 deletions src/__tests__/typescript/generic-model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,22 @@ const createModel = <Item extends ObjectWithId>(): GenericModel<Item> => {
*/

interface SimpleModel<T> {
count: number;
thevalue: T;
theset: Action<SimpleModel<T>, T>;
}

const makeSimpleModel = <T>(initialValue: T): SimpleModel<T> => {
return {
count: 1,
thevalue: initialValue,
theset: action((state, payload) => {
// @ts-ignore
// typings:expect-error
state.count = 1;
// typings:expect-error
state.thevalue = payload;
// typings:expect-error
state.theset();
}),
};
};
Expand All @@ -72,24 +78,30 @@ const makeSimpleModel = <T>(initialValue: T): SimpleModel<T> => {
* WORKAROUND - #300
*/

type Value<T> = [T];

interface SimpleModelWorkaround<T> {
thevalue: Value<T>;
count: number;
thevalue: T;
theset: Action<SimpleModelWorkaround<T>, T>;
}

const makeSimpleModelWorking = <T>(
initialValue: T,
): SimpleModelWorkaround<T> => {
return {
thevalue: [initialValue],
count: 1,
thevalue: initialValue,
theset: action((state, payload) => {
state.thevalue = [payload];
state.count += 1;
state.thevalue = payload;
// typings:expect-error
state.theset('foo');
}),
};
} as SimpleModelWorkaround<any>;
};

const fooModel = makeSimpleModelWorking<string>('foo');
fooModel.thevalue += 'bar';

/**
* #345
*/
Expand Down

0 comments on commit 52c396b

Please sign in to comment.