Closed iva2k closed 7 months ago
TypeScript is aware of CarbonTheme type used for theme property in Theme class, but it does not see it being exported anywhere.
adding this line:
export { CarbonTheme } from "./Theme/Theme.svelte";
to file "types/ index.d.ts" fixes the issue.
Will you take a PR for that 1-liner?
I appreciate the attention to TypeScript DX, but may I suggest any of the following:
const
assertionCasting the string as const narrows the type from a string to the value.
<script lang="ts">
import { Theme } from "carbon-components-svelte";
let theme = "g10" as const;
</script>
ComponentProps
utility type to extract the prop from the component<script lang="ts">
import type { ComponentProps } from "svelte";
import { Theme } from "carbon-components-svelte";
let theme: ComponentProps<Theme>["theme"] = "g10";
</script>
This one looks ugly and is the least future-proof.
<script lang="ts">
import { Theme } from "carbon-components-svelte";
import type { CarbonTheme } from "carbon-components-svelte/src/Theme/Theme.svelte";
let theme: CarbonTheme = "g10";
</script>
@metonym - Thanks! All 3 proposed workaround methods work.
ComponentProps method makes most sense.
Will be the missing type fixed in the package exports?
Will be the missing type fixed in the package exports?
@iva2k The types are currently auto-generated, and it's intentional that these local types (e.g., CarbonTheme
) are not exported from the barrel index file. You can still import it directly, but I want to flag that the path can change in the future (hence why I think it's the least future-proof).
ComponentProps method makes most sense.
I also think the ComponentProps
method is arguably the best as it does not need to care about exported interfaces at all, and it's officially supported by Svelte.
When Theme is used with bind:theme in a .svelte file with TypeScript,
pnpm check
fails:When
theme
is untyped, the error is "Type 'string' is not assignable to type 'CarbonTheme | undefined'."Trying to add
type CarbonTheme
to the imports (to be able to add type totheme
variable also fails).