Skip to content

Commit dc4b719

Browse files
Handle type casting in TypeScript (as) (#83)
* Handle type casting in typescript * changeset
1 parent f79af89 commit dc4b719

File tree

5 files changed

+89
-0
lines changed

5 files changed

+89
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"releases": [{ "name": "extract-react-types", "type": "minor" }],
3+
"dependents": [
4+
{
5+
"name": "babel-plugin-extract-react-types",
6+
"type": "patch",
7+
"dependencies": ["extract-react-types"]
8+
},
9+
{
10+
"name": "extract-react-types-loader",
11+
"type": "patch",
12+
"dependencies": ["extract-react-types"]
13+
},
14+
{ "name": "kind2string", "type": "patch", "dependencies": ["extract-react-types"] },
15+
{
16+
"name": "pretty-proptypes",
17+
"type": "patch",
18+
"dependencies": ["kind2string", "extract-react-types"]
19+
}
20+
]
21+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Adds support for TypeScript's as expression.

packages/extract-react-types/__snapshots__/test.js.snap

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3889,6 +3889,55 @@ Object {
38893889
}
38903890
`;
38913891

3892+
exports[`ts as expression 1`] = `
3893+
Object {
3894+
"component": Object {
3895+
"kind": "generic",
3896+
"name": Object {
3897+
"kind": "id",
3898+
"name": "Component",
3899+
"type": null,
3900+
},
3901+
"value": Object {
3902+
"kind": "object",
3903+
"members": Array [
3904+
Object {
3905+
"default": Object {
3906+
"kind": "string",
3907+
"value": "foo",
3908+
},
3909+
"key": Object {
3910+
"kind": "id",
3911+
"name": "bar",
3912+
},
3913+
"kind": "property",
3914+
"optional": false,
3915+
"value": Object {
3916+
"kind": "generic",
3917+
"value": Object {
3918+
"kind": "union",
3919+
"referenceIdName": "Foo",
3920+
"types": Array [
3921+
Object {
3922+
"kind": "string",
3923+
"value": "foo",
3924+
},
3925+
Object {
3926+
"kind": "string",
3927+
"value": "bar",
3928+
},
3929+
],
3930+
},
3931+
},
3932+
},
3933+
],
3934+
"referenceIdName": "Props",
3935+
},
3936+
},
3937+
"kind": "program",
3938+
}
3939+
`;
3940+
38923941
exports[`ts boolean 1`] = `
38933942
Object {
38943943
"component": Object {

packages/extract-react-types/src/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,6 +1188,10 @@ converters.TSThisType = (path, context): K.This => {
11881188
return { kind: 'custom', value: 'this' };
11891189
};
11901190

1191+
converters.TSAsExpression = (path, context): K.Param => {
1192+
return convert(path.get('expression'), context);
1193+
};
1194+
11911195
function extendedTypesMembers(path, context) {
11921196
const members = path.get('extends');
11931197
if (!members || !members.length) {

packages/extract-react-types/test.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,20 @@ const TESTS = [
503503
}
504504
`
505505
},
506+
{
507+
name: 'ts as expression',
508+
typeSystem: 'typescript',
509+
code: `
510+
type Foo = 'foo' | 'bar';
511+
type Props = { bar: Foo }
512+
513+
class Component extends React.Component<Props> {
514+
static defaultProps = {
515+
bar: 'foo' as Foo,
516+
}
517+
}
518+
`
519+
},
506520
{
507521
name: 'ts object',
508522
typeSystem: 'typescript',

0 commit comments

Comments
 (0)