diff --git a/types/common.d.ts b/types/common.d.ts index 01d1efbfc7a..3454628fb35 100644 --- a/types/common.d.ts +++ b/types/common.d.ts @@ -14,6 +14,16 @@ type Equal = export type HasDefined = Equal extends true ? false : true +type IsAny = Equal extends true ? true : false + +export type HasDefinedAndNotAny = + IsAny extends true + ? false + : Equal extends true + ? false + : true + + // If the type T accepts type "any", output type Y, otherwise output type N. // https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360 export type IfAny = 0 extends 1 & T ? Y : N diff --git a/types/test/utils.ts b/types/test/utils.ts index 48c4fa882df..edf50950179 100644 --- a/types/test/utils.ts +++ b/types/test/utils.ts @@ -5,6 +5,12 @@ export declare function expectType(value: T): void export declare function expectError(value: T): void export declare function expectAssignable(value: T2): void +type NoAny = 0 extends (1 & T) ? never : T; + +export declare function expectTypeNotAny(value: NoAny): void; +export declare function expectErrorNotAny(value: T extends any ? never : T): void; +export declare function expectAssignableNotAny(value: T2 extends any ? never : T2): void; + export type IsUnion = ( T extends any ? (U extends T ? false : true) : never ) extends false diff --git a/types/test/v3/MyTestComponent.js b/types/test/v3/MyTestComponent.js new file mode 100644 index 00000000000..2d0adae7a61 --- /dev/null +++ b/types/test/v3/MyTestComponent.js @@ -0,0 +1,23 @@ +// @ts-check +import { expectTypeNotAny } from '../utils' + +import { + defineComponent, +} from '../../index' + + +export default defineComponent({ + name: 'MyTestComponent', + props: { + a: Number, + }, + data() { + return { + }; + }, + setup(props) { + expectTypeNotAny(props.a) + }, + methods: { + }, +}) \ No newline at end of file diff --git a/types/test/v3/define-component-test.tsx b/types/test/v3/define-component-test.tsx index 7e6d1968ca3..b5385064aca 100644 --- a/types/test/v3/define-component-test.tsx +++ b/types/test/v3/define-component-test.tsx @@ -8,6 +8,7 @@ import { ComponentPublicInstance } from '../../index' import { describe, test, expectType, expectError, IsUnion } from '../utils' +import MyTestComponent from './MyTestComponent' describe('compat with v2 APIs', () => { const comp = defineComponent({}) @@ -264,8 +265,16 @@ describe('with object props', () => { } }) + + expectType(MyTestComponent) expectType(MyComponent) + expectType( + + ) + // Test TSX expectType( ( - options: HasDefined extends true + options: HasDefinedAndNotAny extends true ? { functional?: never } & ComponentOptionsWithProps< PropsOptions, RawBindings,