Skip to content

fix(types): fix props type error with js script #13063

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions types/common.d.ts
Original file line number Diff line number Diff line change
@@ -14,6 +14,16 @@ type Equal<Left, Right> =

export type HasDefined<T> = Equal<T, unknown> extends true ? false : true

type IsAny<T> = Equal<T, any> extends true ? true : false

export type HasDefinedAndNotAny<T> =
IsAny<T> extends true
? false
: Equal<T, unknown> 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<T, Y, N> = 0 extends 1 & T ? Y : N
6 changes: 6 additions & 0 deletions types/test/utils.ts
Original file line number Diff line number Diff line change
@@ -5,6 +5,12 @@ export declare function expectType<T>(value: T): void
export declare function expectError<T>(value: T): void
export declare function expectAssignable<T, T2 extends T = T>(value: T2): void

type NoAny<T> = 0 extends (1 & T) ? never : T;

export declare function expectTypeNotAny<T>(value: NoAny<T>): void;
export declare function expectErrorNotAny<T>(value: T extends any ? never : T): void;
export declare function expectAssignableNotAny<T, T2 extends T = T>(value: T2 extends any ? never : T2): void;

export type IsUnion<T, U extends T = T> = (
T extends any ? (U extends T ? false : true) : never
) extends false
23 changes: 23 additions & 0 deletions types/test/v3/MyTestComponent.js
Original file line number Diff line number Diff line change
@@ -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: {
},
})
9 changes: 9 additions & 0 deletions types/test/v3/define-component-test.tsx
Original file line number Diff line number Diff line change
@@ -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<Component>(MyTestComponent)
expectType<Component>(MyComponent)

expectType<JSX.Element>(
<MyTestComponent
a={1}
></MyTestComponent>
)

// Test TSX
expectType<JSX.Element>(
<MyComponent
1 change: 1 addition & 0 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
"types": ["node"],
"module": "esnext",
"moduleResolution": "node",
"checkJs": true,
"jsx": "preserve",
"strict": true,
"noEmit": true,
4 changes: 2 additions & 2 deletions types/v3-define-component.d.ts
Original file line number Diff line number Diff line change
@@ -16,7 +16,7 @@ import {
ComponentPublicInstanceConstructor,
CreateComponentPublicInstance
} from './v3-component-public-instance'
import { Data, HasDefined } from './common'
import { Data, HasDefined, HasDefinedAndNotAny } from './common'
import { EmitsOptions } from './v3-setup-context'
import { CreateElement, RenderContext } from './umd'

@@ -150,7 +150,7 @@ export function defineComponent<
EmitsNames extends string = string,
PropsOptions extends ComponentPropsOptions = ComponentPropsOptions
>(
options: HasDefined<Props> extends true
options: HasDefinedAndNotAny<Props> extends true
? { functional?: never } & ComponentOptionsWithProps<
PropsOptions,
RawBindings,