Skip to content

Commit 445a56d

Browse files
ianschmitzTimer
authored andcommitted
Fix TypeScript decorator support (#5783)
* Fix TypeScript decorator support * Update babel flow override * WIP
1 parent 2c92fd4 commit 445a56d

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

packages/babel-preset-react-app/create.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,13 @@ module.exports = function(api, opts, env) {
113113
// Strip flow types before any other transform, emulating the behavior
114114
// order as-if the browser supported all of the succeeding features
115115
// https://github.com/facebook/create-react-app/pull/5182
116-
isFlowEnabled &&
116+
// We will conditionally enable this plugin below in overrides as it clashes with
117+
// @babel/plugin-proposal-decorators when using TypeScript.
118+
// https://github.com/facebook/create-react-app/issues/5741
119+
isFlowEnabled && [
117120
require('@babel/plugin-transform-flow-strip-types').default,
121+
false,
122+
],
118123
// Experimental macros support. Will be documented after it's had some time
119124
// in the wild.
120125
require('babel-plugin-macros'),
@@ -177,6 +182,10 @@ module.exports = function(api, opts, env) {
177182
require('babel-plugin-dynamic-import-node'),
178183
].filter(Boolean),
179184
overrides: [
185+
isFlowEnabled && {
186+
exclude: /\.tsx?$/,
187+
plugins: [require('@babel/plugin-transform-flow-strip-types').default],
188+
},
180189
isTypeScriptEnabled && {
181190
test: /\.tsx?$/,
182191
plugins: [

test/fixtures/typescript/src/App.test.ts

+6
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ it('reads a typescript file with no syntax error', () => {
66
expect(App.foo.baz!.n).toBe(123);
77
expect(app.n).toBe(123);
88
});
9+
10+
it('supports decorators', () => {
11+
const app = new App();
12+
expect((app as any).annotated).toBe(true);
13+
expect(app.decorated).toBe(42);
14+
});

test/fixtures/typescript/src/App.ts

+13
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,23 @@ type MyObject = Pick<MyType, 'bar' | 'baz'>;
1010
class App {
1111
static foo: MyObject = { bar: true, baz: { n: 123 } };
1212
n = App.foo.baz!.n;
13+
@propertyDecorator
14+
decorated;
1315
}
1416

1517
function annotation(target: any) {
1618
target.annotated = true;
1719
}
1820

21+
function propertyDecorator(target: any, key: string) {
22+
if (delete target[key]) {
23+
Object.defineProperty(target, key, {
24+
get() {
25+
return 42;
26+
},
27+
enumerable: true,
28+
});
29+
}
30+
}
31+
1932
export default App;

0 commit comments

Comments
 (0)