forked from paypal/glamorous
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(typescript): Improved typings to prevent incorrect usage
The usage of a union between Partials and Functions resulted in any functions passed to glamorous component factories being accepted by the type system. As part of fixing this I also renamed some types to align with the glamorous documentation. BREAKING CHANGE: Renamed types `HTMLGlamorousInterface` -> `HTMLComponentFactory`, `StyledFunction` -> `GlamorousComponentFactory`, `SVGGlamorousInterface` -> `SVGComponentFactory`. Also improved typesafety on them, so things that previously could have passed will now fail. 212
- Loading branch information
Showing
13 changed files
with
654 additions
and
224 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Typescript expected failures 1`] = ` | ||
"yarn test:ts v0.24.6 | ||
$ (tsc -p ./tsconfig.json && rimraf test-ts) || rimraf test-ts | ||
test/glamorous-should-fail.tsx(16,3): error TS2345: Argument of type '{ fillRule: \\"cat\\"; }' is not assignable to parameter of type 'Partial<SVGProperties>'. | ||
Types of property 'fillRule' are incompatible. | ||
Type '\\"cat\\"' is not assignable to type '\\"nonzero\\" | \\"evenodd\\" | \\"inherit\\" | undefined'. | ||
test/glamorous-should-fail.tsx(29,3): error TS2345: Argument of type '() => { fillRule: \\"cat\\"; }' is not assignable to parameter of type 'Partial<SVGProperties>'. | ||
Index signature is missing in type '() => { fillRule: \\"cat\\"; }'. | ||
test/glamorous-should-fail.tsx(43,3): error TS2345: Argument of type '{ float: \\"cat\\"; }' is not assignable to parameter of type 'Partial<CSSProperties>'. | ||
Types of property 'float' are incompatible. | ||
Type '\\"cat\\"' is not assignable to type '\\"left\\" | \\"right\\" | \\"none\\" | \\"initial\\" | \\"inherit\\" | \\"unset\\" | \\"inline-start\\" | \\"inline-end\\" | und...'. | ||
test/glamorous-should-fail.tsx(55,3): error TS2345: Argument of type '() => { float: \\"cat\\"; }' is not assignable to parameter of type 'Partial<CSSProperties>'. | ||
Index signature is missing in type '() => { float: \\"cat\\"; }'. | ||
test/glamorous-should-fail.tsx(71,3): error TS2345: Argument of type '{ fillRule: \\"cat\\"; }' is not assignable to parameter of type 'Partial<CSSProperties>'. | ||
Types of property 'fillRule' are incompatible. | ||
Type '\\"cat\\"' is not assignable to type '\\"nonzero\\" | \\"evenodd\\" | \\"initial\\" | \\"inherit\\" | \\"unset\\" | undefined'. | ||
test/glamorous-should-fail.tsx(77,3): error TS2345: Argument of type '() => { fillRule: \\"cat\\"; }' is not assignable to parameter of type 'Partial<CSSProperties>'. | ||
Index signature is missing in type '() => { fillRule: \\"cat\\"; }'. | ||
test/glamorous-should-fail.tsx(83,3): error TS2345: Argument of type '{ float: \\"cat\\"; }' is not assignable to parameter of type 'Partial<CSSProperties>'. | ||
Types of property 'float' are incompatible. | ||
Type '\\"cat\\"' is not assignable to type '\\"left\\" | \\"right\\" | \\"none\\" | \\"initial\\" | \\"inherit\\" | \\"unset\\" | \\"inline-start\\" | \\"inline-end\\" | und...'. | ||
test/glamorous-should-fail.tsx(89,3): error TS2345: Argument of type '() => { float: \\"cat\\"; }' is not assignable to parameter of type 'Partial<CSSProperties>'. | ||
Index signature is missing in type '() => { float: \\"cat\\"; }'." | ||
`; |
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,18 @@ | ||
var spawnSync = require('child_process').spawnSync | ||
|
||
test('Typescript', () => { | ||
const testTypescriptCompilation = spawnSync('yarn', ['test:ts']) | ||
|
||
const typescriptCompilationOutput = testTypescriptCompilation.stdout | ||
.toString() | ||
.trimRight() | ||
|
||
const trimmedTypescriptCompilationOutput = typescriptCompilationOutput.substring( | ||
0, | ||
typescriptCompilationOutput.lastIndexOf('\n'), | ||
) | ||
|
||
expect(trimmedTypescriptCompilationOutput).toMatchSnapshot( | ||
'Typescript expected failures', | ||
) | ||
}) |
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,92 @@ | ||
import * as React from "react"; | ||
|
||
import glamorous, { withTheme, ThemeProvider } from "../"; | ||
|
||
// built-in DOM component factories | ||
|
||
// ### SVG | ||
|
||
const BuiltInStrictSVGStyleObjectInvalidKey = glamorous.svg( | ||
{ | ||
flying: 'cat', | ||
}, | ||
) | ||
|
||
const BuiltInStrictSVGStyleObjectInvalidProperty = glamorous.svg( | ||
{ | ||
fillRule: 'cat', | ||
}, | ||
) | ||
|
||
// Known to fail currently due to typescripts partial handling | ||
const BuiltInStrictSVGStyleFunctionInvalidKey = glamorous.svg( | ||
() => ({ | ||
flying: 'cat' | ||
}) | ||
) | ||
|
||
const BuiltInStrictSVGStyleFunctionInvalidProperty = glamorous.svg( | ||
() => ({ | ||
fillRule: 'cat', | ||
}) | ||
) | ||
|
||
// ### HTML | ||
|
||
const BuiltInStrictDIVtyleObjectInvlalidKey = glamorous.div( | ||
{ | ||
flying: "cat", | ||
}, | ||
) | ||
|
||
const BuiltInStrictDIVtyleObjectInvlalidProperty = glamorous.div( | ||
{ | ||
float: "cat", | ||
}, | ||
) | ||
|
||
const BuiltInStrictDIVStyleFunctionInvlalidKey = glamorous.div( | ||
() => ({ | ||
flying: "cat", | ||
}) | ||
) | ||
|
||
const BuiltInStrictDIVStyleFunctionInvlalidProperty = glamorous.div( | ||
() => ({ | ||
float: "cat", | ||
}) | ||
) | ||
|
||
// self provided glamorousComponentFactory | ||
|
||
interface TestComponentProps { | ||
className: string | ||
} | ||
|
||
const TestComponent: React.SFC<TestComponentProps> = (props) => ( | ||
<div className={props.className} /> | ||
) | ||
|
||
const StrictSVGStyleObject = glamorous(TestComponent)( | ||
{ | ||
fillRule: 'cat', | ||
}, | ||
) | ||
|
||
const StrictSVGStyleFunction = glamorous(TestComponent)( | ||
() => ({ | ||
fillRule: 'cat', | ||
}) | ||
) | ||
|
||
const BuiltInStrictStyleFunction = glamorous(TestComponent)( | ||
{ | ||
float: "cat", | ||
}, | ||
) | ||
|
||
const BuiltInStrictDivStyleFunction = glamorous(TestComponent)( | ||
() => ({ | ||
float: "cat", | ||
}) | ||
) |
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
Oops, something went wrong.