Skip to content
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

TS typings of components should be more useful #3220

Open
1 task done
CarterLi opened this issue Nov 19, 2020 · 5 comments
Open
1 task done

TS typings of components should be more useful #3220

CarterLi opened this issue Nov 19, 2020 · 5 comments

Comments

@CarterLi
Copy link
Contributor

CarterLi commented Nov 19, 2020

  • I have searched the issues of this repository and believe that this is not a duplicate.

What problem does this feature solve?

(this.$refs.input as Input).focus();

results in error:

ERROR in src/views/home/home.tsx:16:28
TS2749: 'Input' refers to a value, but is being used as a type here. Did you mean 'typeof Input'?
14 |   methods: {
15 |     fn() {
> 16 |       (this.$refs.input as Input).focus();
|                            ^^^^^
17 |     }
18 |   }
19 | });

Instead, you have to write the verbose and not-newbie-friendly code:

(this.$refs.input as InstanceType<typeof Input>).focus();

That works with vscode intellisense and type checking.

What does the proposed API look like?

(this.$refs.input as Input).focus();

should work as the old version does.

@zkwolf
Copy link
Member

zkwolf commented Nov 19, 2020

vuejs/rfcs#210 似乎可以期待一下这个API?

@chsword
Copy link
Contributor

chsword commented Nov 21, 2020

可以这样,针对组件 Input 设计一个 InputInterface 的接口

interface InputInterface {
//这里对于 需要公开外部处理的methods 添加 方法签名,如
setValue(value: string | number, callback?: Function) ;
focus();
}

之后在调用时

defineComponent({
         setup(){
              const input =ref<InputInterface>(null);
              return { input }
         },
       methods: {
             fn() {
                  this.input.focus();
              }
       }
})
<a-input ref="input" />

@zkwolf
Copy link
Member

zkwolf commented Nov 21, 2020

看上去应该可以

@github-actions
Copy link

This issue is stale because it has been open 60 days with no activity. Remove stale label or comment or this will be closed in 7 days

@github-actions github-actions bot added the Stale label Jan 21, 2021
@CarterLi
Copy link
Contributor Author

Unstale please

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants