Skip to content

Type error when using context API #212

Closed
@Hagendorn

Description

@Hagendorn

Hi,

I got a type error when using the context API.

Component:

<script lang="ts">
  import { getContext } from "svelte";

  const foo = getContext("foo");

  console.log({ foo });
</script>

{foo}

Test:

import { render } from "@testing-library/svelte";

import Comp from "./Component.svelte";

it("gets context", () => {
  render(Comp, { context: new Map([["foo", "bar"]]) });
});

Running svelte-check reports this type error:

▶ npm run check 

> vite-svelte-context-type-issue@0.0.0 check
> svelte-check --tsconfig ./tsconfig.json


====================================
Loading svelte-check in workspace: /Users/user/workspace/vite-svelte-context-type-issue
Getting Svelte diagnostics...

/Users/user/workspace/vite-svelte-context-type-issue/src/Component.test.ts:6:18
Error: No overload matches this call.
  Overload 1 of 2, '(component: Constructor<Component__SvelteComponent_>, componentOptions?: SvelteComponentOptions<Component__SvelteComponent_>, renderOptions?: Omit<...>): RenderResult<...>', gave the following error.
    Type 'Map<string, string>' is not assignable to type 'never'.
  Overload 2 of 2, '(component: Constructor<Component__SvelteComponent_>, componentOptions?: SvelteComponentOptions<Component__SvelteComponent_>, renderOptions?: RenderOptions<...>): RenderResult<...>', gave the following error.
    Type 'Map<string, string>' is not assignable to type 'never'. 
it("gets context", () => {
  render(Comp, { context: new Map([["foo", "bar"]]) });
});


====================================
svelte-check found 1 error, 0 warnings, and 0 hints

I created a sample repository to reproduce this issue:
https://github.com/Hagendorn/vite-svelte-context-type-issue

To reproduce it checkout the repository and run:

> npm i
> npm run check

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions