TypeScriptλ₯Ό μ¬μ©ν λ μ»΄ν¬λνΈλ₯Ό μ νμΌλ‘ μ¬μ©νλ κ²μ λΆκ°λ₯ ν΄ λ³΄μ
λλ€.
μλ₯Ό λ€μ΄ λ€μκ³Ό κ°μ΄ κ΅¬μ± μμ μΈμ€ν΄μ€λ₯Ό 보μ νκΈ° μν΄ μ μ₯μλ₯Ό μ
λ ₯ ν μ μμ΅λλ€.
import AnyComponent from "./AnyComponent"
const componentStore = writable<SvelteComponent>(null)
$componentStore = AnyComponent
λ§μ§λ§ μ€μ λ€μ μ€λ₯λ₯Ό μ 곡ν©λλ€.
μ ν 'typeof \
κ΅¬μ± μμλ₯Ό μ λ ₯νλ λ°©λ²μ λν΄ λμΉ κ²μ΄ λμΌ μλ μμ§λ§ κ·Έλ° μ’ λ₯μ ν¨ν΄μ μ¬μ©ν μ μκΈ°λ₯Ό λ°λλλ€.
λ€μμμ΄ λ¬Έμ λ₯Ό μμ±νκΈ° μ μ μ΄μ΄ λ³Έ StackOverflow κ²μλ¬Όμ λλ€ . @dummdidumm μ΄μ΄ λ¬Έμ λ₯Ό μ΄
μλ
νμΈμ π
ν κ°μ§λ Svelte μ»΄ν¬λνΈ ν΄λμ€μ λ΄λΆ μ μμ λν μΌλΆ μμ± / λ©μλκ° λλ½λμλ€λ κ²μ
λλ€. λ€λ₯Έ κ²μ μ
λ ₯μ΄ μλͺ»λμλ€λ κ²μ
λλ€. writable<typeof SvelteComponent>
μ΄μ΄μΌν©λλ€. κ·Έλ μ§ μμΌλ©΄ ν΄λμ€ μ νμ΄ μλ ν΄λΉ κ΅¬μ± μμμ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°ν©λλ€.
λΉμ·ν λ¬Έμ κ° λ°μνμ΅λλ€.
import type { SvelteComponent } from 'svelte';
import Comp from './Comp.svelte';
const comp: SvelteComponent = Comp; // Type 'typeof SvelteComponentDev' is missing the following properties from type SvelteComponentDev: $set, $on, $destroy ...
v 3.35.0
λ¬Έμ λ κ°μ Έμ¨ κ΅¬μ± μμκ° SvelteComponentDevκ° μλ "typeof SvelteComponentDev"μ νμ κ°λ μ΄μ μ λλ€.
Comp
λ ν΄λΉ ν΄λμ€μ μΈμ€ν΄μ€κ° μλλΌ ν΄λμ€μ΄κΈ° λλ¬Έμ
λλ€.
class Foo {
bar: string;
}
class Comp extends Foo {}
const comp: Foo = new Comp(); // OK
const compClass1: Foo = Comp; // WRONG
const compClass2: typeof Foo = Comp; // OK
μ. λ§μ΄λλ€. κ°μ¬ν©λλ€.
μ§κΈ μ΄ν λμ μ΄κ±Έλ‘ μΈμ°κ³ μμ΅λλ€. λλ κ·Έκ²μ μ΄ν΄νμ§ λͺ»νλ€.
<script lang="ts">
import MyComponent from "./MyComponent.svelte";
let myComponent: typeof MyComponent;
</script>
<MyComponent bind:this={myComponent}/> //Error here
Argument of type 'typeof MyComponent__SvelteComponent_' is not assignable to parameter of type 'MyComponent__SvelteComponent_'.
Type 'typeof MyComponent__SvelteComponent_' is missing the following properties from type 'MyComponent__SvelteComponent_': $$prop_def, $$events_def, $$slot_def, $on, and 5 more.ts(2345)
λ΄κ° μ¬κΈ°μ λ μλͺ»νκ³ μλ? κ·Έ μΈμ€ν΄μ€λ₯Ό λ³μλ‘ κ°μ Έμ€κ³ μΆμ΅λλ€. π
@ Alfagun74 λΉμ μ΄ μ§λ©΄ ν λ¬Έμ λμ΄ μ€λ λμ λ€λ₯Έ λ¬Έμ μ λμΌνμ§ μμ΅λλ€. μνλ κ²μ ν΄λμ€μ μΈμ€ν΄μ€μ λλ€. λ°λΌμ λ€μμ΄ νμν©λλ€.
<script lang="ts">
import MyComponent from "./MyComponent.svelte";
let myComponent: MyComponent;
</script>
<MyComponent bind:this={myComponent}/>
λ€λ₯Έ μ¬λλ€μ λμ κ΅¬μ± μμλ₯Ό μνλ―λ‘ ν΄λμ€ μ체, νΉμ ν¨μκ° νμν©λλ€.
κ°μ₯ μ μ©ν λκΈ
μλ νμΈμ π
ν κ°μ§λ Svelte μ»΄ν¬λνΈ ν΄λμ€μ λ΄λΆ μ μμ λν μΌλΆ μμ± / λ©μλκ° λλ½λμλ€λ κ²μ λλ€. λ€λ₯Έ κ²μ μ λ ₯μ΄ μλͺ»λμλ€λ κ²μ λλ€.
writable<typeof SvelteComponent>
μ΄μ΄μΌν©λλ€. κ·Έλ μ§ μμΌλ©΄ ν΄λμ€ μ νμ΄ μλ ν΄λΉ κ΅¬μ± μμμ μΈμ€ν΄μ€λ₯Ό μ°Έμ‘°ν©λλ€.