-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
feat: add Prop to main type declaration file [#6850] #6856
Conversation
This should also fix #6841. I wonder how it would impact case like Of course, exporting more types means more compatibility for us to maintain. If it can also fix |
@HerringtonDarkholme it seems to work fine with union types where you want the constructor instead of the primitive: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! This pull request does fix some problems!
union props can be fully typed now!
To deal with union type, I guess it would be more convenient to declare another type (e.g. type PropType<T> = Prop<T> | Prop<T>[]
interface Foo {
a: string
}
Vue.extend({
props: {
// Both looks the same syntax
// (we don't need to concern the `type` is an array or not)
foo: Object as PropType<Foo>
union: [Object, String] as PropType<Foo | string>
}
}) |
|
@HerringtonDarkholme, I think that @ktsn has a good point. That is technically the purpose of props: {
myProp: {/* constructor, array, or prop options */} as PropValidator<IMyProp>
} |
@HerringtonDarkholme But the @ferdaber |
The only difference between props: {
union: [Object, String] as PropType<Foo | string>,
complexUnion: {
type: [Object, String] as PropType<Foo | string>
// ... other stuff
}
} versus props: {
union: [Object, String] as PropValidator<Foo | string>,
complexUnion: {
type: [Object, String]
// ... other stuff
} as PropValidator<Foo | string>
} I guess to me it makes more sense to have the typecast be in the same level of each prop definition, but it doesn't really make much of a difference so I'm fine with whichever you all decide. The other benefit I see too would be that |
I think |
Great! I will modify the PR to create a new type called |
types/options.d.ts
Outdated
export type PropValidator<T> = PropOptions<T> | Prop<T> | Prop<T>[]; | ||
export type PropType<T> = Prop<T> | Prop<T>[]; | ||
|
||
export type PropValidator<T> = PropOptions<T> | PropType<T>; | ||
|
||
export interface PropOptions<T=any> { | ||
type?: Prop<T> | Prop<T>[]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should the type here be PropType<T>
?
Does this PR solve the prop compile error: Compiler Error ERROR in src/js/components/Manage/DataViz/HdSalesGraph.vue.ts
[tsl] ERROR in src/js/components/Manage/DataViz/HdSalesGraph.vue.ts(71,26)
TS2339: Property 'currentMoment' does not exist on type 'CombinedVueInstance<Vue, { styles: { width: string; height: string; position: string; }; }, { for...'.
ERROR in src/js/components/Manage/DataViz/HdSalesGraph.vue.ts
[tsl] ERROR in src/js/components/Manage/DataViz/HdSalesGraph.vue.ts(75,26)
TS2339: Property 'previousMoment' does not exist on type 'CombinedVueInstance<Vue, { styles: { width: string; height: string; position: string; }; }, { for...'. Abbreviated Source <script lang="ts">
import Vue from "vue";
import { Prop } from "vue/types/options";
import moment from "moment";
export default Vue.extend({
name: "HdSalesGraph",
props: {
currentMoment: {
type: Object as Prop<moment.Moment>,
required: true,
},
previousMoment: {
type: Object as Prop<moment.Moment>,
required: true,
},
},
data() {
return {
styles: {
width: "100%",
height: "500px",
position: "relative",
},
};
},
computed: {
curMoment(): moment.Moment {
return moment(this.currentMoment);
// ^^^^^^^^^^^^^ 71,26
},
prevMoment(): moment.Moment {
return moment(this.previousMoment);
// ^^^^^^^^^^^^^^ 75,26
},
},
});
</script> |
Will this change solve this issue: #7640? Because in the TypeScript repo they've said that it works as intended: microsoft/TypeScript#22471 |
Any updates on this? |
Any movement on this? Keen to see it land! |
Do we know when this will be released? I am sure "when it's ready" but just looking for a guestimate? |
@sodatea Do you know when this will be released? |
Do we know when this will be released? This very important for us. |
@yyx990803 It's been a month since your last comment. Any hope of getting this merged soon? I'm glad there's a workaround (by simply excluding |
@ffxsam Looking forward to this as well, but is the function interface syntax not a viable workaround? Types are working for me with: type: Array as () => string[], |
@ky-is Nice trick, I'll give it a try. Thanks! |
Hi, I'm facing this problem as well. The code is pretty simple:
I get the following error (on line
Hope we'll find a solution soon. |
The solution is this PR. I'm not sure what the holdup is. Is it pending review? |
@yyx990803 @ferdaber can this be merged in? It's a big roadblock for our TypeScript codebase, too! |
I think what's happening is that they won't focus on this kind of issues as they are rewritting Vue in TypeScript (as announced here: https://medium.com/the-vue-point/plans-for-the-next-iteration-of-vue-js-777ffea6fabf). |
@cesalberca -- that is all well and good, but realistically it may be a year before adoption of V3.0 etc. meanwhile people everyday are going through this pain -- which is FIXED -- and an easy patch. It doesn't make sense. |
Maybe just just make a local copy and patch it yourself and not wait forever? That what we did. |
I know @sjmcdowall , I'm just saying that this is what may be occurring. I wouldn't agree too if this is whats truly happening. |
will this be merged so soon? |
Fantastic!
Is this the appropriate way to do it? |
@abelgoodwin1988, I think you should use it like that
The PropType interface definition is
|
I've been having good luck with PropOptions as an alternative approach. <script lang="ts">
import Vue, { PropOptions } from 'vue';
import { Education } from '@/assets/resume/resume.model';
export default Vue.extend({
props: {
education: Array,
} as PropOptions<Education[]>,
}); The name might be singular, I can't check now from my phone. I like this approach because it also hints the validator function parameters. |
ok |
What kind of change does this PR introduce? (check at least one)
Does this PR introduce a breaking change? (check one)
If yes, please describe the impact and migration path for existing applications:
The PR fulfills these requirements:
dev
branch for v2.x (or to a previous version branch), not themaster
branchfix #xxx[,#xxx]
, where "xxx" is the issue number)If adding a new feature, the PR's description includes:
See #6850.
Other information: