Skip to content

Commit

Permalink
feat(compiler-sfc): support ExtractPropTypes when resolving types
Browse files Browse the repository at this point in the history
close #8104
  • Loading branch information
yyx990803 committed Apr 20, 2023
1 parent 5c69895 commit 50c0bbe
Show file tree
Hide file tree
Showing 2 changed files with 303 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,56 @@ describe('resolveType', () => {
})
})

test('typeof', () => {

This comment has been minimized.

Copy link
@jd-solanki

jd-solanki Apr 27, 2023

I just noticed you improved this, do these improvements resolve this issue?

I tried 3.3 beta 2 and found that:

const props = { foo: String}
export type Props = ExtractPropTypes<typeof props>

Give the correct type:

type Props = {} & {
    foo?: string | undefined;
}

However, with boolean:

const props = { foo: Boolean}
export type Props = ExtractPropTypes<typeof props>

It's required:

type Props = {
    foo: boolean;
} & {}
expect(
resolve(`
declare const a: string
defineProps<{ foo: typeof a }>()
`).props
).toStrictEqual({
foo: ['String']
})
})

test('ExtractPropTypes (element-plus)', () => {
const { props, raw } = resolve(
`
import { ExtractPropTypes } from 'vue'
declare const props: {
foo: StringConstructor,
bar: {
type: import('foo').EpPropFinalized<BooleanConstructor>,
required: true
}
}
type Props = ExtractPropTypes<typeof props>
defineProps<Props>()
`
)
expect(props).toStrictEqual({
foo: ['String'],
bar: ['Boolean']
})
expect(raw.props.bar.optional).toBe(false)
})

test('ExtractPropTypes (antd)', () => {
const { props } = resolve(
`
declare const props: () => {
foo: StringConstructor,
bar: { type: PropType<boolean> }
}
type Props = Partial<import('vue').ExtractPropTypes<ReturnType<typeof props>>>
defineProps<Props>()
`
)
expect(props).toStrictEqual({
foo: ['String'],
bar: ['Boolean']
})
})

describe('external type imports', () => {
const files = {
'/foo.ts': 'export type P = { foo: number }',
Expand Down Expand Up @@ -659,6 +709,7 @@ function resolve(
return {
props,
calls: raw.calls,
deps: ctx.deps
deps: ctx.deps,
raw
}
}
Loading

0 comments on commit 50c0bbe

Please sign in to comment.