Skip to content

Commit f66e5ed

Browse files
authored
fix(types): remove language prop type from HighlightAuto (#217)
* fix(types): use 42781Events interface to type dispatched events * fix(types): remove language prop from HighlightAuto 42781Props interface * refactor(types): reuse Slots interface * test: assert packaged types
1 parent 9ac4f49 commit f66e5ed

File tree

4 files changed

+38
-29
lines changed

4 files changed

+38
-29
lines changed

src/Highlight.svelte

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,22 @@
88
register: LanguageFn;
99
}
1010
11+
export interface Slots {
12+
default: {
13+
/**
14+
* The highlighted code.
15+
*/
16+
highlighted: HighlightedCode;
17+
};
18+
}
19+
1120
export interface Events {
12-
highlight: {
21+
highlight: CustomEvent<{
22+
/**
23+
* The highlighted code.
24+
*/
1325
highlighted?: HighlightedCode;
14-
};
26+
}>;
1527
}
1628
</script>
1729

@@ -35,11 +47,9 @@
3547
langtag?: boolean;
3648
}
3749
38-
interface $$Slots {
39-
default: {
40-
highlighted: HighlightedCode;
41-
};
42-
}
50+
interface $$Slots extends Slots {}
51+
52+
interface $$Events extends Events {}
4353
4454
export let language: Language = {
4555
name: undefined,
@@ -53,7 +63,7 @@
5363
import hljs from "highlight.js/lib/core";
5464
import { createEventDispatcher, afterUpdate } from "svelte";
5565
56-
const dispatch = createEventDispatcher<Events>();
66+
const dispatch = createEventDispatcher();
5767
5868
let highlighted: HighlightedCode = undefined;
5969

src/HighlightAuto.svelte

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script context="module" lang="ts">
2-
import type { HighlightedCode, Language, Events } from "./Highlight.svelte";
2+
import type { HighlightedCode, Slots, Events } from "./Highlight.svelte";
33
</script>
44

55
<script lang="ts">
@@ -9,24 +9,16 @@
99
*/
1010
code?: any;
1111
12-
/**
13-
* Provide the language to highlight the code.
14-
* Import languages from `svelte-highlight/languages/*`.
15-
*/
16-
language?: Language;
17-
1812
/**
1913
* Set to `true` for the language name to be
2014
* displayed at the top right of the code block.
2115
*/
2216
langtag?: boolean;
2317
}
2418
25-
interface $$Slots {
26-
default: {
27-
highlighted: HighlightedCode;
28-
};
29-
}
19+
interface $$Slots extends Slots {}
20+
21+
interface $$Events extends Events {}
3022
3123
export let code = undefined;
3224
@@ -35,7 +27,7 @@
3527
import hljs from "highlight.js";
3628
import { createEventDispatcher, afterUpdate } from "svelte";
3729
38-
const dispatch = createEventDispatcher<Events>();
30+
const dispatch = createEventDispatcher();
3931
4032
let highlighted: HighlightedCode = undefined;
4133
let language = undefined;

src/HighlightSvelte.svelte

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script context="module" lang="ts">
2-
import type { HighlightedCode, Events } from "./Highlight.svelte";
2+
import type { Slots, Events } from "./Highlight.svelte";
33
</script>
44

55
<script lang="ts">
@@ -16,11 +16,9 @@
1616
langtag?: boolean;
1717
}
1818
19-
interface $$Slots {
20-
default: {
21-
highlighted: HighlightedCode;
22-
};
23-
}
19+
interface $$Slots extends Slots {}
20+
21+
interface $$Events extends Events {}
2422
2523
export let code = undefined;
2624
@@ -32,7 +30,7 @@
3230
import css from "highlight.js/lib/languages/css";
3331
import { createEventDispatcher, afterUpdate } from "svelte";
3432
35-
const dispatch = createEventDispatcher<Events>();
33+
const dispatch = createEventDispatcher();
3634
3735
hljs.registerLanguage("xml", xml);
3836
hljs.registerLanguage("javascript", javascript);

tests/SvelteHighlightPackage.test.svelte

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,16 @@
77
import "../package/styles/3024.css";
88
</script>
99

10-
<Highlight code="" language={javascript || typescript} />
10+
<Highlight
11+
code=""
12+
language={javascript || typescript}
13+
on:highlight={(e) => {
14+
console.log(e.detail);
15+
}}
16+
let:highlighted
17+
>
18+
{@html highlighted}
19+
</Highlight>
1120

1221
<svelte:component this={Highlight2} />
1322

0 commit comments

Comments
 (0)