types: ensure props are readonly

This commit is contained in:
Evan You
2019-11-09 18:40:25 -05:00
parent a5f962ab8e
commit 08bf9976ae
4 changed files with 28 additions and 15 deletions

View File

@@ -14,7 +14,10 @@ import { VNodeProps } from './vnode'
// 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,

View File

@@ -83,7 +83,7 @@ export type ComponentOptionsWithoutProps<
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,
@@ -91,7 +91,7 @@ export type ComponentOptionsWithArrayProps<
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>>
@@ -102,7 +102,7 @@ export type ComponentOptionsWithObjectProps<
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>>

View File

@@ -65,14 +65,8 @@ export type ExtractPropTypes<
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 {