Skip to content

fix(dialogs/spawnDialog): improve typing#6770

Closed
ShGKme wants to merge 2 commits into
mainfrom
fix/spawnDialog--types
Closed

fix(dialogs/spawnDialog): improve typing#6770
ShGKme wants to merge 2 commits into
mainfrom
fix/spawnDialog--types

Conversation

@ShGKme
Copy link
Copy Markdown
Contributor

@ShGKme ShGKme commented Apr 11, 2025

☑️ Resolves

// Props are checked to be valid MyDialog props
// result type is inferred
const result = await spawnDialog<typeof MyDialog>(MyDialog, { prop: 'value' })

// But doesn't work this way :(
const result = await spawnDialog(MyDialog, { prop: 'value' })

🖼️ Screenshots

🏚️ Before 🏡 After
B A

🚧 Tasks

  • ...

🏁 Checklist

  • ⛑️ Tests are included or are not applicable
  • 📘 Component documentation has been extended, updated or is not applicable
  • 2️⃣ Backport to stable8 for maintained Vue 2 version or not applicable

@ShGKme ShGKme added bug Something isn't working 2. developing Work in progress feature: functions composables, functions, mixins and other non-components labels Apr 11, 2025
@ShGKme ShGKme added this to the v9.0.0-rc.0 milestone Apr 11, 2025
@ShGKme ShGKme self-assigned this Apr 11, 2025
[skip ci]
Comment on lines +50 to +55
export function spawnDialog<
C extends Component,
Props extends Record<EventPropName<'close'>, (...payload: unknown[]) => unknown> = ComponentProps<C>, // eslint-disable-line space-before-function-paren
Payload extends unknown[] = EventPayload<NonNullable<Props[EventPropName<'close'>]>>,
>(
dialog: C,
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why it works, we C is specified as typeof MyDialog, but it cannot infer it from MyDialog passed to the dialog param...

@ShGKme
Copy link
Copy Markdown
Contributor Author

ShGKme commented Apr 15, 2025

For manual testing:

const ComponentNoEvent = defineComponent({
	name: 'Component1',
	props: {
		foo: {
			type: Number,
			required: true,
		},
	},
})

const Component1 = defineComponent({
	name: 'Component1',
	props: {
		foo: Number,
	},
	emits: {
		close: () => true,
	},
})

const Component2 = defineComponent({
	name: 'Component2',
	props: {
		foo: Number,
	},
	emits: {
		close: (payload: Date) => true,
		open: () => true,
	},
})

const Component3 = defineComponent({
	name: 'Component3',
	props: {
		foo: Number,
	},
	emits: {
		close: (a: number, b: number) => true,
	},
})

const result0 = await spawnDialog(ComponentNoEvent, { foo: 1 }) // Should be never

const result1 = await spawnDialog(Component1, { foo: 1 }) // Should be void

const result2 = await spawnDialog(Component2, { foo: 1 })
console.log(result2[0].toISOString())

const result3 = await spawnDialog(Component3, { foo: 1 })
console.log(-result3[0] - result3[1])

@ShGKme
Copy link
Copy Markdown
Contributor Author

ShGKme commented Apr 15, 2025

@ShGKme ShGKme closed this Apr 15, 2025
@susnux susnux deleted the fix/spawnDialog--types branch September 10, 2025 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2. developing Work in progress bug Something isn't working feature: functions composables, functions, mixins and other non-components

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant