-
Notifications
You must be signed in to change notification settings - Fork 545
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
Different external and internal names for props #10
base: master
Are you sure you want to change the base?
Conversation
Also at vuejs/vue#7943 |
a14c18f
to
e5024ed
Compare
I like the idea, nice work! One thing I'm concerned about (but not qualified to answer) is how this complicates typings for Typescript. |
Nice. I think props: {
ageOfPerson: {
type: Number,
alias: 'age'
}
} |
Hmm, if we call it So for a component {
props: {
fullName: {
type: String,
alias: 'name',
}
} these would have to result in identical output: If we don't want an alias but "to have different external and internal names" then TL;DR: If it's an actual alias and both names are valid when passing props, call it |
this is great! I like the initial idea "as", since it is intuitive coming from js module imports import { externalName as name } from 'someModule' Agree with @jonaskuske that if it's "alias", then it looks like the prop can be used by either name, which can be confusing since props are usually a form of documentation. |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
For real aliases you can already utilise computed properties props: ['external'],
computed: {
internal () {
return this.external
}
} Sure this is a little bit verbose but it elegantly arises from Vues natural capabilities. EDIT 1: ok sorry, I didn't read the full RFC. EDIT 2: Hmm... I still think I have a point though. You just really really want to keep using the external prop name even when you plug a computed property in-between. I think that's a rather trivial use case though. |
There is an API available for provide/inject feature to have different local name, I think we should use similar API for props.
|
@JosephSilber Stateful prop defaults
Prop coercion
|
I think the examples list in the Motivation section of the RFC (as well as some use cases commented) are not good arguments for this proposal.
props: ['initialCounter'],
data() {
return {
counter: this.initialCounter
}
}
props: ['size'],
computed: {
normalizedSize() {
return this.size.trim().toLowerCase()
}
} With the introduction of Composition API, those use cases could be handled elegantly: props: ['size'],
setup(props) {
const size = computed(() => {
return props.size.trim().toLowerCase()
})
return {
size
}
} That being said, I'm not entirely against this. The Swift example in the RFC regarding argument labels is what in the similar vein with this proposal. props: {
person: String,
from: {
as: 'hometown',
type: String
}
} And take the example of the new props: {
to: {
type: [String, Element]
}
},
setup(props) {
const target = toRef(props, 'to')
// setting up
return {
target
}
} But it's not as expressive and cohesive as following IMO: props: {
to: {
as: 'target',
type: [String, Element]
}
},
setup(props) {
// setting up
return {
//
}
} So, in summary:
|
Just to clarify: it's not possible to solve that with Composition, you'll get an error for using a property with the same name as in props. I think this change complicates things a lot more than it actually helps a developer. TypeScript support would be poor for this and probably require manually typing your props. It would also be unclear what value is being used in context without looking at props declaration first. Additionally since you can't mutate props but probably are going to use that value as an initial value in data that alone introduces confusion: component takes a prop foo then internally mutates foo context value. It's much more clear when we have foo as a prop and currentFoo as an internal state. Lastly, it can be mitigated by wrapping internal state into an object, which I think has a well balanced developer experience. |
#513 |
View fully rendered RFC
Allow aliasing props, to have different external and internal names (some languages call these "argument labels"):