Skip to content

Commit

Permalink
stop using StringKeyOf
Browse files Browse the repository at this point in the history
  • Loading branch information
wonderful-panda committed Nov 20, 2018
1 parent 8b7aa33 commit b18e949
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 19 deletions.
25 changes: 11 additions & 14 deletions src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ThisTypedComponentOptionsWithRecordProps as ThisTypedComponentOptions
} from "vue/types/options";

import { TsxComponentAttrs, ScopedSlots, StringKeyOf } from "../types/base";
import { TsxComponentAttrs, ScopedSlots } from "../types/base";
export { TsxComponentAttrs, ScopedSlots } from "../types/base";
import { EventsNativeOn, AllHTMLAttributes } from "../types/dom";
export { EventsNativeOn, AllHTMLAttributes } from "../types/dom";
Expand Down Expand Up @@ -111,16 +111,13 @@ export function withUnknownProps<VC extends typeof Vue>(
// `{ foo: String, bar: String, baz: { type: String, required: true as true} }`
// then, `RequiredPropNames<typeof props>` is "baz",
export type RequiredPropNames<PropsDef extends RecordPropsDefinition<any>> = ({
[K in StringKeyOf<PropsDef>]: PropsDef[K] extends { required: true }
? K
: never
})[StringKeyOf<PropsDef>];
[K in keyof PropsDef]: PropsDef[K] extends { required: true } ? K : never
})[Extract<keyof PropsDef, string>];

export type PropsForOutside<
Props,
RequiredPropNames extends StringKeyOf<Props>
> = { [K in RequiredPropNames]: Props[K] } &
{ [K in Exclude<StringKeyOf<Props>, RequiredPropNames>]?: Props[K] };
export type PropsForOutside<Props, RequiredPropNames extends keyof Props> = {
[K in RequiredPropNames]: Props[K]
} &
{ [K in Exclude<keyof Props, RequiredPropNames>]?: Props[K] };

export interface ComponentFactory<
BaseProps,
Expand All @@ -132,8 +129,8 @@ export interface ComponentFactory<
create<
Props,
PropsDef extends RecordPropsDefinition<Props>,
RequiredProps extends StringKeyOf<Props> = RequiredPropNames<PropsDef> &
StringKeyOf<Props>
RequiredProps extends keyof Props = RequiredPropNames<PropsDef> &
keyof Props
>(
options: FunctionalComponentOptions<
Props,
Expand All @@ -154,8 +151,8 @@ export interface ComponentFactory<
Computed,
Props,
PropsDef extends RecordPropsDefinition<Props>,
RequiredProps extends StringKeyOf<Props> = RequiredPropNames<PropsDef> &
StringKeyOf<Props>
RequiredProps extends keyof Props = RequiredPropNames<PropsDef> &
keyof Props
>(
options: ThisTypedComponentOptions<
AdditionalThisAttrs & Super & Vue,
Expand Down
8 changes: 3 additions & 5 deletions types/base.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ declare global {
}
}

export type StringKeyOf<T> = Extract<keyof T, string>;

export type KnownAttrs = Pick<
VNodeData,
"class" | "staticClass" | "key" | "ref" | "slot" | "scopedSlots"
Expand All @@ -24,13 +22,13 @@ export type KnownAttrs = Pick<
domPropsInnerHTML?: string;
};
export type ScopedSlots<T> = {
[K in StringKeyOf<T>]: (props: T[K]) => VNodeChildrenArrayContents | string
[K in keyof T]: (props: T[K]) => VNodeChildrenArrayContents | string
} & {
[name: string]: (props: any) => VNodeChildrenArrayContents | string;
};

export type EventHandlers<E> = {
[K in StringKeyOf<E>]?: E[K] extends Function ? E[K] : (payload: E[K]) => void
[K in keyof E]?: E[K] extends Function ? E[K] : (payload: E[K]) => void
};

export type TsxComponentAttrs<TProps = {}, TEvents = {}, TScopedSlots = {}> =
Expand Down Expand Up @@ -59,7 +57,7 @@ export interface ElementAttributesProperty {
}

export type IntrinsicElements = {
[K in StringKeyOf<dom.IntrinsicElementAttributes>]: ElementAttrs<
[K in keyof dom.IntrinsicElementAttributes]: ElementAttrs<
dom.IntrinsicElementAttributes[K]
>
};

0 comments on commit b18e949

Please sign in to comment.