-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Despite precisely knowing for a given `preferenceName` what its associated type for `newValue` and `oldValue` should be, the current typing wasn't allowing TypeScript to properly infer the type. With this commit, we can do things like: ```ts declare const event: PreferenceChangeEvent<{ a: string, b: number }> if (event.preferenceName === 'a') { event.newValue // type is `string` } ``` It is worth explaining the new type for the `PreferenceChangeEvent`: ```ts // Given T: type T = { a: string, b: number } // We construct a new type such as: type U = { a: { preferenceName: 'a' newValue: string oldValue?: string } b: { preferenceName: 'b' newValue: number oldValue?: number } } // Then we get the union of all values of U by selecting by `keyof T`: type V = U[keyof T] // Implementation: type PreferenceChangeEvent<T> = { // Create a mapping where each key is a key from T, // -? normalizes optional typings to avoid getting // `undefined` as part of the final union: [K in keyof T]-?: { // In this object, K will take the value of each // independent key from T: preferenceName: K newValue: T[K] oldValue?: T[K] // Finally we create the union by doing so: }[keyof T] } ``` The type is more complex, but it now represents how it is meant to be used rather than getting tsserver lost like before. This commit also fixes places where the typing was broken and makes use of the new information. Signed-off-by: Paul Maréchal <[email protected]>
- Loading branch information
1 parent
6798167
commit 42bee66
Showing
12 changed files
with
39 additions
and
34 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
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
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
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