Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can TypeScript understand Svelte components?

Svelte ultimately outputs native JavaScript classes. So, TypeScript could understand these. However, Svelte components have to first be compiled from their initial .html form. Until then, understandably, TypeScript by default does not understand them in their initial form. So, it reports a 'cannot find module' error, even though at runtime the import is successful. Is there any way to make TypeScript understand them?

One workaround would be to provide type definitions for .html modules, approximating the standard Svelte component interface. However, it would be much more desirable to simply use the real component class output itself for each individual component, yielding the most accurate type information.

As an aside, I have not mentioned tools like Webpack or Rollup which perform the compilation step for Svelte normally. I do not know whether these tools would be relevant to this question.

Update 1: I've looked a little further into TypeScript and it seems that plugins can be created for it. However, they seem limited, so may not be useful.

Update 2: There has also been some talk (here and here) about custom module loaders in TypeScript.

like image 501
Chris Talman Avatar asked Jan 07 '18 16:01

Chris Talman


People also ask

Is Svelte JavaScript or TypeScript?

Svelte is, of course, written in TypeScript, so the project strongly believes in the value of typing. An offical blog post announced full support for it in mid 2020. Here is how to get started: First you need to change you build setup.

Does Svelte use components?

In Svelte, an application is composed from one or more components. A component is a reusable, self-contained block of code that encapsulates HTML, CSS, and JavaScript that belong together, written into a . svelte file.

Does Svelte use JavaScript?

Svelte is a compiler that generates minimal and highly optimized JavaScript code from our sources; you'll need a terminal with node + npm installed to compile and build your app.

Why Svelte is the best?

Svelte has a higher satisfaction rate than React or Vue and is the most interesting front-end framework in the last 3 years. Svelte is the most loved framework among 66k responders. Svelte provides developers with one of the highest satisfaction rates among developers.

How do I use typescript with svelte?

When we say that Svelte now supports TypeScript, we mean a few different things: You can use TypeScript inside your <script> blocks — just add the lang="ts" attribute Components with TypeScript can be type-checked with the svelte-check command

How do I use typescript inside a <script> block?

You can use TypeScript inside your <script> blocks — just add the lang="ts" attribute Components with TypeScript can be type-checked with the svelte-check command You get autocompletion hints and type-checking as you're writing components, even in expressions inside markup

What is first class TypeScript support?

First class TypeScript support means that both of these two systems do a good job of handling TypeScript code. The Svelte compiler support for TypeScript is handled by Christian Kaisermann 's svelte-preprocess which is now an official Svelte project. For the editor level, we took inspiration from Pine's work in the Vue ecosystem via Vetur.

Does TypeScript support structural typing?

TypeScript supports structural typing. Structural typing is a way of relating types based solely on their members, even if you do not explicitly define the type. We'll define a TodoType type to see how TypeScript enforces that anything passed to a component expecting a TodoType will be structurally compatible with it.


Video Answer


2 Answers

Yes. Use rollup-plugin-svelte and rollup-plugin-typescript and it should work. I am using it in a rollup file now that looks as follows:

import commonjs from 'rollup-plugin-commonjs'
import replace from 'rollup-plugin-replace'
import resolve from 'rollup-plugin-node-resolve'
import svelte from 'rollup-plugin-svelte'
import typescript from 'rollup-plugin-typescript'

const path = require('path')
const fs = require('fs')

export default {
  input: 'src/index.ts',
  plugins: [
    typescript(),
    commonjs({
      include: 'node_modules/**'
    }),
    resolve({
      browser: true,
      preferBuiltins: false // for url npm module; otherwise rollup assumes node
    }),
    // Replace is to shut up redux
    replace({
      'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
    }),
    svelte({
      // By default, all .html and .svelte files are compiled
      extensions: ['.my-custom-extension', '.html', '.svelte'],

      // You can restrict which files are compiled
      // using `include` and `exclude`
      // include: 'src/components/**/*.html',

      // By default, the client-side compiler is used. You
      // can also use the server-side rendering compiler
      // generate: 'ssr',

      // Extract CSS into a separate file (recommended).
      css: function (css) {
        fs.writeFileSync('./dist/svelte.css', css)
      }
    })
  ],
  output: {
    format: 'iife',
    file: path.join(__dirname, './dist/index_dist.js') // equivalent to --output
  }
}
like image 130
Scott Willeke Avatar answered Oct 23 '22 15:10

Scott Willeke


Yes. Svelte has official support for typescript! https://svelte.dev/blog/svelte-and-typescript

The starter template comes with a setupTypeScript.js utility: https://github.com/sveltejs/template#using-typescript

At this time it doesn't work in combination with eslint, but that is being worked on

like image 38
Bob Fanger Avatar answered Oct 23 '22 15:10

Bob Fanger