-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
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
Allow constraining ref attribute to not allow string when using tsx. #3455
Comments
This should work: defineComponent({
setup() {
const dom = ref(null)
return {
dom
}
},
render() {
return <div ref="dom"></div>
}
}) what do you actually mean? |
Using jsx-next, recommended here, for writing tsx files and using this syntax. import { defineComponent } from "vue";
defineComponent({
setup() {
const dom = ref(null)
return () => (
<div ref={dom} />
);
},
}); |
But according to the example I provided above, it allows specifying ref as a string |
jsx-next provides two syntaxes for writing render functions. It does work the way you wrote it but it does not work the way I wrote it. I prefer returning a render function from setup as it's more succinct and typescript tells me when a custom component is not within the template scope. jsx-next offers a way to write it the way I like so I'd just like to be able to control what is allowed for ref values on native input elements. For those that choose this method for their whole application they should be able to restrict what is passed to ref to suit their style. // Bulkier syntax
import { defineComponent } from "vue";
import CustomComponent from './CustomComponent'
defineComponent({
components: {
CustomComponent
}
setup() {
const dom = ref(null)
return {
dom,
title: 'info'
}
},
render () {
return (
<div ref="dom" >
<CustomComponent title={this.title} />
</div>
);
}
}); // more succinct syntax
import { defineComponent } from "vue";
import CustomComponent from './CustomComponent'
defineComponent({
setup() {
const dom = ref(null)
const title = 'info'
return () => (
<div ref={dom}>
<CustomComponent title={title} />
</div>
);
},
}); |
To be clear this doesn't work and I'd like typescript to tell me what what is being passed to ref is an error import { defineComponent } from "vue";
import CustomComponent from './CustomComponent'
defineComponent({
setup() {
const dom = ref(null)
const title = 'info'
return () => (
<div ref="dom">
<CustomComponent title={title} />
</div>
);
},
}); |
I know what you mean: you mean that if jsx is used as the return value of setup then it is not allowed to be specified as a string, otherwise, it can. But we cannot restrict ref to not be able to specify a string, because it actually allows the use of string types. And I don’t think we can limit ref correctly unless we can analyze its context correctly. |
I'm not saying that vue should limit it for everyone. I'm saying that consumers of vue should be able to customize it for their own purposes. For example, by adding this to my application type definition files I fix this issue for custom components, because it ends up overriding what is defined in import * as RuntimeCore from '@vue/runtime-core'
declare module '@vue/runtime-core' {
declare interface ComponentCustomProps {
ref?:
| RuntimeCore.Ref
| ((ref: Element | RuntimeCore.ComponentInternalInstance | null) => void)
}
} now typescript will hint that ref is incorrect below import { defineComponent } from "vue";
import CustomComponent from './CustomComponent'
defineComponent({
setup() {
const customRef = ref(null)
return () => (
<CustomComponent ref="customRef" />
);
},
}); I'd like the same ability to define a type definition file in my application to override what is in |
Thank you @HcySunYang |
What problem does this feature solve?
When writing a component in tsx, passing a string to a
ref
attribute on a native element does not work and typescript does not tell you that anything is wrong.This is not a problem with custom components because we can override
ref
typing like:What does the proposed API look like?
I'm not sure the best way to solve this problem but here is where the problem is:
The text was updated successfully, but these errors were encountered: