Skip to content

Commit 96ac54b

Browse files
committed
feat: export ComponentRenderProxy for tsx
Resolves: #123
1 parent 64ea4a6 commit 96ac54b

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

README.md

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [Installation](#Installation)
1414
- [Usage](#Usage)
1515
- [TypeScript](#TypeScript)
16+
- [TSX](#tsx)
1617
- [Limitations](#Limitations)
1718
- [API](https://vue-composition-api-rfc.netlify.com/api.html)
1819
- [Changelog](https://github.com/vuejs/composition-api/blob/master/CHANGELOG.md)
@@ -54,7 +55,9 @@ After installing the plugin you can use the [Composition API](https://vue-compos
5455

5556
# TypeScript
5657

57-
**Please upgrade to the latest TypeScript. If you are using vetur, make sure to set `vetur.useWorkspaceDependencies` to `true`.**
58+
We provide an Example [Repository](https://github.com/liximomo/vue-composition-api-tsx-example) with TS and TSX support to help you start.
59+
60+
**This plugin requires TypeScript version >3.5.1. If you are using vetur, make sure to set `vetur.useWorkspaceDependencies` to `true`.**
5861

5962
To let TypeScript properly infer types inside Vue component options, you need to define components with `createComponent`:
6063

@@ -71,6 +74,31 @@ const Component = {
7174
};
7275
```
7376

77+
## TSX
78+
79+
To support TSX, create a declaration file with following content in your project.
80+
81+
```ts
82+
// file: shim-tsx.d.ts
83+
import Vue, { VNode } from 'vue';
84+
import { ComponentRenderProxy } from '@vue/composition-api';
85+
86+
declare global {
87+
namespace JSX {
88+
// tslint:disable no-empty-interface
89+
interface Element extends VNode {}
90+
// tslint:disable no-empty-interface
91+
interface ElementClass extends ComponentRenderProxy {}
92+
interface ElementAttributesProperty {
93+
$props: any; // specify the property name to use
94+
}
95+
interface IntrinsicElements {
96+
[elem: string]: any;
97+
}
98+
}
99+
}
100+
```
101+
74102
# Limitations
75103

76104
## `Ref` Unwrap
@@ -150,10 +178,10 @@ b.list[1].count === 1; // true
150178
## Template Refs
151179

152180
> :white_check_mark:
153-
Support     :x: Not Supported
181+
> Support     :x: Not Supported
154182
155183
:white_check_mark:
156-
String ref && return it from `setup()`:
184+
String ref && return it from `setup()`:
157185

158186
```html
159187
<template>
@@ -179,7 +207,7 @@ b.list[1].count === 1; // true
179207
```
180208

181209
:white_check_mark:
182-
String ref && return it from `setup()` && Render Function / JSX:
210+
String ref && return it from `setup()` && Render Function / JSX:
183211

184212
```jsx
185213
export default {

README.zh-CN.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
- [安装](#安装)
1414
- [使用](#使用)
1515
- [TypeScript](#TypeScript)
16+
- [TSX](#tsx)
1617
- [限制](#限制)
1718
- [API](https://vue-composition-api-rfc.netlify.com/api.html)
1819
- [Changelog](https://github.com/vuejs/composition-api/blob/master/CHANGELOG.md)
@@ -54,6 +55,8 @@ Vue.use(VueCompositionApi);
5455

5556
# TypeScript
5657

58+
我们提供了一个配置好 TC/TSX 支持的[示例仓库](https://github.com/liximomo/vue-composition-api-tsx-example)来帮助你快速开始.
59+
5760
**请使用最新版的 TypeScript,如果你使用了 `vetur`,请将 `vetur.useWorkspaceDependencies` 设为 `true`**
5861

5962
为了让 TypeScript 正确的推导类型,我们必须使用 `createComponent` 来定义组件:
@@ -71,6 +74,31 @@ const Component = {
7174
};
7275
```
7376

77+
## TSX
78+
79+
要支持 TSX,请创建一个类型定义文件并提供正确的 JSX 定义。内容如下:
80+
81+
```ts
82+
// file: shim-tsx.d.ts`
83+
import Vue, { VNode } from 'vue';
84+
import { ComponentRenderProxy } from '@vue/composition-api';
85+
86+
declare global {
87+
namespace JSX {
88+
// tslint:disable no-empty-interface
89+
interface Element extends VNode {}
90+
// tslint:disable no-empty-interface
91+
interface ElementClass extends ComponentRenderProxy {}
92+
interface ElementAttributesProperty {
93+
$props: any; // specify the property name to use
94+
}
95+
interface IntrinsicElements {
96+
[elem: string]: any;
97+
}
98+
}
99+
}
100+
```
101+
74102
# 限制
75103

76104
## `Ref` Unwrap

src/component/component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export type ComponentInstance = InstanceType<VueConstructor>;
99

1010
// public properties exposed on the proxy, which is used as the render context
1111
// in templates (as `this` in the render option)
12-
type ComponentRenderProxy<P = {}, S = {}, PublicProps = P> = {
12+
export type ComponentRenderProxy<P = {}, S = {}, PublicProps = P> = {
1313
$data: S;
1414
$props: PublicProps;
1515
$attrs: Data;

src/component/index.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
1-
export { Data, createComponent, SetupFunction, SetupContext, ComponentInstance } from './component';
1+
export {
2+
Data,
3+
createComponent,
4+
SetupFunction,
5+
SetupContext,
6+
ComponentInstance,
7+
ComponentRenderProxy,
8+
} from './component';
29
export { PropType, PropOptions } from './componentProps';

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ if (currentVue && typeof window !== 'undefined' && window.Vue) {
2323
export default plugin;
2424
export { default as createElement } from './createElement';
2525
export { SetupContext };
26-
export { createComponent, PropType, PropOptions } from './component';
26+
export { createComponent, ComponentRenderProxy, PropType, PropOptions } from './component';
2727

2828
export * from './apis/state';
2929
export * from './apis/lifecycle';

0 commit comments

Comments
 (0)