-
-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
110 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { O } from 'ts-toolbelt'; | ||
|
||
/** | ||
* ARRANGE | ||
*/ | ||
|
||
interface Person { | ||
name: string; | ||
} | ||
|
||
type StateMapper<Model extends object> = { | ||
[P in keyof Model]: Model[P]; | ||
}; | ||
|
||
type State<Model extends object> = StateMapper<O.Filter<Model, Action<any>>>; | ||
|
||
type Action<Model extends object> = (state: State<Model>) => void; | ||
|
||
/** | ||
* WORKING CASE - "NORMAL" MODEL INTERFACE | ||
*/ | ||
|
||
interface NormalModel { | ||
data: Person[]; | ||
fetch: Action<NormalModel>; | ||
} | ||
|
||
const normalModel: NormalModel = { | ||
data: [], | ||
fetch: state => { | ||
// 👍 works | ||
state.data[0].name; | ||
}, | ||
}; | ||
|
||
/** | ||
* BROKEN CASE - "GENERIC" MODEL INTERFACE | ||
*/ | ||
|
||
interface ObjectWithId { | ||
id: string; | ||
} | ||
|
||
interface GenericModel<Item extends ObjectWithId> { | ||
data: Item[]; | ||
fetch: Action<GenericModel<Item>>; | ||
} | ||
|
||
const createModel = <Item extends ObjectWithId>(): GenericModel<Item> => { | ||
return { | ||
data: [], | ||
fetch: state => { | ||
// 👇 broken | ||
state.data; | ||
}, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters