// overload 1: direct setup function
// (uses user defined props interface)
export function createComponent<Props, RawBindings = object>(
- setup: (props: Props, ctx: SetupContext) => RawBindings | RenderFunction
+ setup: (
+ props: Readonly<Props>,
+ ctx: SetupContext
+ ) => RawBindings | RenderFunction
): {
new (): ComponentPublicInstance<
Props,
M extends MethodOptions = {}
> = ComponentOptionsBase<Props, RawBindings, D, C, M> & {
props?: undefined
-} & ThisType<ComponentPublicInstance<{}, RawBindings, D, C, M, Props>>
+} & ThisType<ComponentPublicInstance<{}, RawBindings, D, C, M, Readonly<Props>>>
export type ComponentOptionsWithArrayProps<
PropNames extends string = string,
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
- Props = { [key in PropNames]?: any }
+ Props = Readonly<{ [key in PropNames]?: any }>
> = ComponentOptionsBase<Props, RawBindings, D, C, M> & {
props: PropNames[]
} & ThisType<ComponentPublicInstance<Props, RawBindings, D, C, M>>
D = {},
C extends ComputedOptions = {},
M extends MethodOptions = {},
- Props = ExtractPropTypes<PropsOptions>
+ Props = Readonly<ExtractPropTypes<PropsOptions>>
> = ComponentOptionsBase<Props, RawBindings, D, C, M> & {
props: PropsOptions
} & ThisType<ComponentPublicInstance<Props, RawBindings, D, C, M>>
O,
MakeDefaultRequired extends boolean = true
> = O extends object
- ? {
- readonly [K in RequiredKeys<O, MakeDefaultRequired>]: InferPropType<O[K]>
- } &
- {
- readonly [K in OptionalKeys<O, MakeDefaultRequired>]?: InferPropType<
- O[K]
- >
- }
+ ? { [K in RequiredKeys<O, MakeDefaultRequired>]: InferPropType<O[K]> } &
+ { [K in OptionalKeys<O, MakeDefaultRequired>]?: InferPropType<O[K]> }
: { [K in string]: any }
const enum BooleanFlags {
expectType<ExpectedProps['ccc']>(props.ccc)
expectType<ExpectedProps['ddd']>(props.ddd)
+ // props should be readonly
+ expectError((props.a = 1))
+
// should also expose declared props on `this`
expectType<ExpectedProps['a']>(this.a)
expectType<ExpectedProps['b']>(this.b)
expectType<ExpectedProps['ccc']>(this.ccc)
expectType<ExpectedProps['ddd']>(this.ddd)
+ // props on `this` should be readonly
+ expectError((this.a = 1))
+
// assert setup context unwrapping
expectType<number>(this.c)
expectType<string>(this.d.e)
+ // setup context properties should be mutable
+ this.c = 2
+
return null
}
})
},
render() {
expectType<string>(this.$props.msg)
+ // props should be readonly
+ expectError((this.$props.msg = 'foo'))
+ // should not expose on `this`
expectError(this.msg)
expectType<number>(this.a)
return null
createComponent({
props: ['a', 'b'],
setup(props) {
- props.a
- props.b
+ // props should be readonly
+ expectError((props.a = 1))
+ expectType<any>(props.a)
+ expectType<any>(props.b)
return {
c: 1
}
},
render() {
- expectType<{ a?: any; b?: any }>(this.$props)
+ expectType<any>(this.$props.a)
+ expectType<any>(this.$props.b)
+ expectError((this.$props.a = 1))
expectType<any>(this.a)
expectType<any>(this.b)
expectType<number>(this.c)