Skip to content

Commit 7cdcad9

Browse files
committed
fix: type errors after making site package strict
1 parent d2eaac0 commit 7cdcad9

File tree

10 files changed

+49
-37
lines changed

10 files changed

+49
-37
lines changed

packages/checkbox/Checkbox.svelte

+16-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
bind:checked={nativeChecked}
4343
data-indeterminate={!isUninitializedValue(indeterminate) && indeterminate
4444
? 'true'
45-
: null}
45+
: undefined}
4646
on:blur
4747
on:focus
4848
{...nativeControlAttrs}
@@ -92,7 +92,7 @@
9292
export let touch = false;
9393
export let indeterminate: UninitializedValue | boolean = uninitializedValue;
9494
export let group: UninitializedValue | any[] = uninitializedValue;
95-
export let checked: UninitializedValue | boolean = uninitializedValue;
95+
export let checked: UninitializedValue | boolean | null = uninitializedValue;
9696
export let value: any = null;
9797
export let valueKey: UninitializedValue | string = uninitializedValue;
9898
export let input$use: ActionArray = [];
@@ -107,10 +107,10 @@
107107
let rippleActive = false;
108108
let inputProps =
109109
getContext<{ id?: string } | undefined>('SMUI:generic:input:props') ?? {};
110-
let nativeChecked = isUninitializedValue(group)
110+
let nativeChecked: boolean | undefined = isUninitializedValue(group)
111111
? isUninitializedValue(checked)
112112
? false
113-
: checked
113+
: checked ?? undefined
114114
: group.indexOf(value) !== -1;
115115
let context = getContext<string | undefined>('SMUI:checkbox:context');
116116
let dataTableHeader = getContext<boolean | undefined>(
@@ -160,19 +160,22 @@
160160
161161
// Now check individual state.
162162
if (isUninitializedValue(checked)) {
163-
if (previousNativeChecked !== nativeChecked) {
163+
if (!!previousNativeChecked !== !!nativeChecked) {
164164
// The checkbox was clicked by the user.
165165
callHandleChange = true;
166166
}
167-
} else if (checked !== nativeChecked) {
167+
} else if (checked !== (nativeChecked ?? null)) {
168168
if (checked === previousChecked) {
169169
// The checkbox was clicked by the user
170170
// and the change needs to flow up.
171-
checked = nativeChecked;
171+
checked = nativeChecked ?? null;
172+
if (!isUninitializedValue(indeterminate)) {
173+
indeterminate = false;
174+
}
172175
} else {
173176
// The checkbox was changed programmatically
174177
// and the change needs to flow down.
175-
nativeChecked = checked;
178+
nativeChecked = checked ?? undefined;
176179
}
177180
callHandleChange = true;
178181
}
@@ -205,12 +208,15 @@
205208
}
206209
207210
onMount(() => {
211+
checkbox.indeterminate =
212+
!isUninitializedValue(indeterminate) && indeterminate;
213+
208214
instance = new MDCCheckboxFoundation({
209215
addClass,
210216
forceLayout: () => element.offsetWidth,
211217
hasNativeControl: () => true,
212218
isAttachedToDOM: () => Boolean(element.parentNode),
213-
isChecked: () => nativeChecked,
219+
isChecked: () => nativeChecked ?? false,
214220
isIndeterminate: () =>
215221
isUninitializedValue(indeterminate) ? false : indeterminate,
216222
removeClass,
@@ -225,7 +231,7 @@
225231
return getElement();
226232
},
227233
get checked() {
228-
return nativeChecked;
234+
return nativeChecked ?? false;
229235
},
230236
set checked(value) {
231237
if (nativeChecked !== value) {

packages/common/useActions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export type SvelteActionReturnType<P> = {
44
} | void;
55

66
export type SvelteActionType<P> = (
7-
node: Element,
7+
node: HTMLElement,
88
params?: P
99
) => SvelteActionReturnType<P>;
1010

@@ -14,7 +14,7 @@ export type ActionEntry<P extends any = any> =
1414

1515
export type ActionArray = ActionEntry[];
1616

17-
export function useActions(node: Element, actions: ActionArray) {
17+
export function useActions(node: HTMLElement, actions: ActionArray) {
1818
let actionReturns: SvelteActionReturnType<any>[] = [];
1919

2020
if (actions) {

packages/select/Select.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@
261261
export let variant: 'standard' | 'filled' | 'outlined' = 'standard';
262262
export let noLabel = false;
263263
export let label: string | undefined = undefined;
264-
export let value = '';
264+
export let value: any = '';
265265
export let key: (item: any) => string = (item) => item;
266266
export let dirty = false;
267267

packages/site/src/lib/Demo.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
8585
export let file: string | undefined = undefined;
8686
export let files: string[] = typeof file === 'string' ? [file] : [];
87-
export let component: SvelteComponent | string;
87+
export let component: typeof SvelteComponent | string;
8888
8989
let sourceContainer: HTMLDivElement | undefined = undefined;
9090
let show = false;

packages/site/src/routes/__layout.svelte

+10-9
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<Title
1515
component={A}
1616
href="/"
17-
on:click={() => (activeSection = null)}
17+
on:click={() => (activeSection = undefined)}
1818
class="mdc-theme--primary"
1919
style={miniWindow ? 'padding-left: 0;' : ''}
2020
>
@@ -110,7 +110,7 @@
110110
? section.route
111111
: 'shortcut' in section
112112
? section.shortcut
113-
: null}
113+
: undefined}
114114
activated={section === activeSection}
115115
style={section.indent
116116
? 'margin-left: ' + section.indent * 25 + 'px;'
@@ -177,8 +177,8 @@
177177
}
178178
themeLink.href = `/smui${lightTheme ? '' : '-dark'}.css`;
179179
document.head
180-
.querySelector('link[href="/smui-dark.css"]')
181-
.insertAdjacentElement('afterend', themeLink);
180+
.querySelector<HTMLLinkElement>('link[href="/smui-dark.css"]')
181+
?.insertAdjacentElement('afterend', themeLink);
182182
183183
let siteLink = document.head.querySelector<HTMLLinkElement>('#site');
184184
if (!siteLink) {
@@ -188,8 +188,8 @@
188188
}
189189
siteLink.href = `/site${lightTheme ? '' : '-dark'}.css`;
190190
document.head
191-
.querySelector('link[href="/site-dark.css"]')
192-
.insertAdjacentElement('afterend', siteLink);
191+
.querySelector<HTMLLinkElement>('link[href="/site-dark.css"]')
192+
?.insertAdjacentElement('afterend', siteLink);
193193
}
194194
195195
type DemoSection = {
@@ -509,9 +509,10 @@
509509
];
510510
511511
$: activeSection = sections.find(
512-
(section) => 'route' in section && routesEqual(section.route, $page.path)
513-
) as DemoSection;
514-
let previousPagePath = null;
512+
(section) =>
513+
'route' in section && routesEqual(section.route ?? '', $page.path)
514+
) as DemoSection | undefined;
515+
let previousPagePath: string | undefined = undefined;
515516
$: if (mainContent && previousPagePath !== $page.path) {
516517
drawerOpen = false;
517518
const hashEl =

packages/site/src/routes/demo/checkbox/_Indeterminate.svelte

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
<FormField>
22
<!--
33
Note: binding to `indeterminate` is probably a bad idea.
4-
The component will never set `indeterminate` internally.
4+
The component will set `indeterminate`, but it should be
5+
always related to `checked`.
6+
7+
`indeterminate` is required for the correct state, even
8+
if `checked` is null.
59
-->
610
<Checkbox bind:checked indeterminate={checked === null} input$required />
711
<span slot="label">I agree to the terms.</span>
@@ -17,5 +21,5 @@
1721
import FormField from '@smui/form-field';
1822
import Button from '@smui/button';
1923
20-
let checked = null;
24+
let checked: boolean | null = null;
2125
</script>

packages/site/src/routes/demo/common/_ForwardEventsBuilder.svelte

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
let key = 'None';
1818
let focused = false;
1919
20-
function handleKeyPress(event: CustomEvent & KeyboardEvent) {
20+
const handleKeyPress = (((event: KeyboardEvent) => {
2121
key = event.key;
22-
}
22+
}) as unknown) as (event: CustomEvent) => void;
2323
</script>

packages/site/src/routes/demo/list/_TwoLineSelection.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
let selection = 'Stephen Hawking';
6767
// This value is updated when the component is initialized, based on the
6868
// selected Item's `selected` prop.
69-
let selectionIndex = null;
69+
let selectionIndex: number | undefined = undefined;
7070
</script>
7171

7272
<style>

packages/site/src/routes/demo/menu-surface/_ManualAnchor.svelte

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@
4444
4545
let surface: MenuSurface;
4646
let anchor: HTMLDivElement;
47-
let anchorClasses = {};
47+
let anchorClasses: { [k: string]: boolean } = {};
4848
</script>

packages/site/src/routes/demo/select/_Objects.svelte

+9-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
</Select>
1818

1919
<pre
20-
class="status">Selected: {valueA ? valueA.label : 'None'}, Price: {valueA ? valueA.price : '0'}¢</pre>
20+
class="status">Selected: {valueA ? valueA.label : 'None'}, Price: {valueA ? valueA.price : '-'}¢</pre>
2121
</div>
2222

2323
<div>
@@ -34,7 +34,7 @@
3434
</Select>
3535

3636
<pre
37-
class="status">Selected: {valueB ? valueB.label : 'None'}, Price: {valueB ? valueB.price : '0'}¢</pre>
37+
class="status">Selected: {valueB ? valueB.label : 'None'}, Price: {valueB ? valueB.price : '-'}¢</pre>
3838
</div>
3939

4040
<div>
@@ -44,28 +44,29 @@
4444
bind:value={valueC}
4545
label="Outlined"
4646
>
47-
<Option value={null} />
47+
<Option value={undefined} />
4848
{#each fruits as fruit (fruit.label)}
4949
<Option value={fruit}>{fruit.label}</Option>
5050
{/each}
5151
</Select>
5252

5353
<pre
54-
class="status">Selected: {valueC ? valueC.label : 'None'}, Price: {valueC ? valueC.price : '0'}¢</pre>
54+
class="status">Selected: {valueC ? valueC.label : 'None'}, Price: {valueC ? valueC.price : '-'}¢</pre>
5555
</div>
5656
</div>
5757

5858
<script lang="ts">
5959
import Select, { Option } from '@smui/select';
6060
61-
let fruits = [
61+
type Fruit = { label: string; price: number };
62+
let fruits: Fruit[] = [
6263
{ label: 'Apple', price: 35 },
6364
{ label: 'Orange', price: 38 },
6465
{ label: 'Banana', price: 28 },
6566
{ label: 'Mango', price: 25 },
6667
];
6768
68-
let valueA = null;
69-
let valueB = null;
70-
let valueC = null;
69+
let valueA: Fruit | undefined = undefined;
70+
let valueB: Fruit | undefined = undefined;
71+
let valueC: Fruit | undefined = undefined;
7172
</script>

0 commit comments

Comments
 (0)