-
Notifications
You must be signed in to change notification settings - Fork 133
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve TypeSafety #16
Merged
Merged
Changes from 3 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
d67f459
Improve type-safety by using generics
META-DREAMER 3bb338d
Update TypeScript version and migrate from eslint-plugin-shopify to @…
META-DREAMER f901f00
Update gitignore for Jetbrains IDE folder
META-DREAMER 1ddc915
Remove Omit helper
META-DREAMER 382fbdd
Improve TypeSafety of useRestyle hook
META-DREAMER d3af8bc
Cleanup / improve typings of useRestyle and RestyleFunction
META-DREAMER c09dd1b
Improve typings for restyleFunctions and createRestyleFunction to hav…
META-DREAMER 436a67e
Improve typings of createRestyleComponent to include generics, allowi…
META-DREAMER 730deb8
Improve typesafety of createVariant
META-DREAMER c72487d
Fix type safety of themeKey
META-DREAMER 4a48eaa
Fix createVariant types to work when no property is defined
META-DREAMER 5da7874
Fix README typo
META-DREAMER eb862e3
Improve typesafety of createVariant using overloads
META-DREAMER File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,6 @@ npm-debug.log | |
.jest | ||
|
||
dist | ||
|
||
# IDE | ||
.idea |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,11 +5,10 @@ import { | |
RestyleFunctionContainer, | ||
} from './types'; | ||
|
||
type StyleTransformFunction = (params: { | ||
value: any; | ||
theme: BaseTheme; | ||
themeKey?: string; | ||
}) => any; | ||
type StyleTransformFunction< | ||
Theme extends BaseTheme, | ||
K extends keyof Theme | ||
> = (params: {value: any; theme: Theme; themeKey?: K}) => any; | ||
|
||
const getValueForScreenSize = ({ | ||
responsiveValue, | ||
|
@@ -41,7 +40,7 @@ const isResponsiveObjectValue = <Theme extends BaseTheme>( | |
}, true); | ||
}; | ||
|
||
const getValue = <Theme extends BaseTheme>( | ||
const getValue = <Theme extends BaseTheme, K extends keyof Theme>( | ||
propValue: ResponsiveValue<string | number, Theme>, | ||
{ | ||
theme, | ||
|
@@ -50,9 +49,9 @@ const getValue = <Theme extends BaseTheme>( | |
themeKey, | ||
}: { | ||
theme: Theme; | ||
transform?: StyleTransformFunction; | ||
transform?: StyleTransformFunction<Theme, K>; | ||
dimensions: Dimensions; | ||
themeKey?: string; | ||
themeKey?: K; | ||
}, | ||
) => { | ||
const val = isResponsiveObjectValue(propValue, theme) | ||
|
@@ -73,26 +72,28 @@ const getValue = <Theme extends BaseTheme>( | |
return val; | ||
}; | ||
|
||
const createRestyleFunction = ({ | ||
const createRestyleFunction = < | ||
TProps = Record<string, unknown>, | ||
Theme extends BaseTheme = BaseTheme, | ||
P extends keyof TProps = keyof TProps, | ||
K extends keyof Theme = keyof Theme | ||
>({ | ||
property, | ||
transform, | ||
styleProperty = property, | ||
styleProperty = property.toString(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since objects can have numbered or Symbol properties, but the styleProperty type is a string, we need to convert it to a string if we want to assign it like this by default. |
||
themeKey, | ||
}: { | ||
property: string; | ||
transform?: StyleTransformFunction; | ||
property: P; | ||
transform?: StyleTransformFunction<Theme, K>; | ||
styleProperty?: string; | ||
themeKey?: string; | ||
}): RestyleFunctionContainer => { | ||
themeKey?: K; | ||
}): RestyleFunctionContainer<TProps, Theme, P, K> => { | ||
return { | ||
property, | ||
themeKey, | ||
variant: false, | ||
func: ( | ||
props: any, | ||
{theme, dimensions}: {theme: BaseTheme; dimensions: Dimensions}, | ||
): {[key: string]: any} => { | ||
const value = getValue(props[property], { | ||
func: (props, {theme, dimensions}): Record<string, any> => { | ||
const value = getValue<Theme, K>(props[property], { | ||
theme, | ||
dimensions, | ||
themeKey, | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"include": [ | ||
"./src/**/*.ts", | ||
"./src/**/*.tsx", | ||
"**/*.test.ts", | ||
"**/*.test.tsx" | ||
], | ||
"exclude": [ | ||
"node_modules" | ||
] | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a valid case for when you should be able to override P and K here? If not, I would prefer to just using
keyof TProps
andkeyof Theme
inline within the function.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Its not so much about overriding as it is about having a strongly typed return type so that the exact property and themeKey the user specified is preserved and carried through to anything consuming that restyle function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah right, got it