refactor: use conditional types

This commit is contained in:
madkarmaa 2025-04-08 09:19:42 +02:00
parent e36417bfb2
commit 63e0d63d4a
2 changed files with 24 additions and 5 deletions

View File

@ -1,21 +1,36 @@
<script lang="ts">
import type { Component, Snippet } from 'svelte';
import type { SpecialTargetValues } from '$types';
import type { SpecialTargetValues, Prettify } from '$types';
type ButtonType = 'filled' | 'tonal' | 'text' | 'outlined';
type Props = {
type BaseProps = {
type: ButtonType;
icon?: Component;
iconSize?: number;
iconColor?: string;
href?: string;
target?: `${SpecialTargetValues}` | SpecialTargetValues;
label?: string;
children?: Snippet;
onclick?: () => void;
};
type ButtonProps = Prettify<
BaseProps & {
onclick: () => void;
href: never;
target: never;
}
>;
type LinkProps = Prettify<
BaseProps & {
href: string;
target?: `${SpecialTargetValues}` | SpecialTargetValues;
onclick: never;
}
>;
type Props = ButtonProps | LinkProps;
let {
type,
// https://svelte.dev/docs/svelte/compiler-warnings#svelte_component_deprecated

4
src/lib/types.d.ts vendored
View File

@ -7,3 +7,7 @@ export enum SpecialTargetValues {
PARENT = '_parent',
TOP = '_top'
}
type Prettify<T> = {
[K in keyof T]: T[K];
} & {};