question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't compile the quickstart on typescript

See original GitHub issue

Description I’m following the quickstart guide with typescript and it will not compile.

Here’s my code, as per the quickstart:

import React, { useEffect, useMemo, useState} from 'react';
import { render } from 'react-dom';
import { BaseEditor, createEditor } from 'slate';
import { Slate, ReactEditor, Editable, withReact } from 'slate-react';

type CustomElement = { type: 'paragraph'; children: CustomText[] }
type CustomText = { text: string }

declare module 'slate' {
  interface CustomTypes {
    Editor: BaseEditor & ReactEditor
    Element: CustomElement
    Text: CustomText
  }
}

const App = () => {
  const editor = useMemo(() => withReact(createEditor()), [])
  // Add the initial value when setting up our state.
  const [value, setValue] = useState([
    {
      type: 'paragraph',
      children: [{ text: 'A line of text in a paragraph.' }],
    },
  ])

  return (
    <Slate
      editor={editor}
      value={value}
      onChange={newValue => setValue(newValue)}
    >
      <Editable />
    </Slate>
  )
}

render(<App />, document.getElementById('root'));

Here are the errors I get:

TS2322: Type '{ type: string; children: { text: string; }[]; }[]' is not assignable to type 'Descendant[]'.
  Type '{ type: string; children: { text: string; }[]; }' is not assignable to type 'Descendant'.
    Type '{ type: string; children: { text: string; }[]; }' is not assignable to type 'CustomElement'.
      Types of property 'type' are incompatible.
        Type 'string' is not assignable to type '"paragraph"'.

TS2345: Argument of type 'Descendant[]' is not assignable to parameter of type 'SetStateAction<{ type: string; children: { text: string; }[]; }[]>'.
  Type 'Descendant[]' is not assignable to type '{ type: string; children: { text: string; }[]; }[]'.
    Type 'Descendant' is not assignable to type '{ type: string; children: { text: string; }[]; }'.
      Type 'CustomText' is missing the following properties from type '{ type: string; children: { text: string; }[]; }': type, children

Here is my tsconfig:

{
  "compilerOptions": {
	"module": "commonjs",
	"noImplicitAny": true,
	"removeComments": true,
	"preserveConstEnums": true,
	"sourceMap": true,
	"esModuleInterop": true,
	"jsx": "react"
  },
  "include": ["src/ts/*"],
}

Environment

  • Slate Version: 0.63.0
  • Operating System: macOS
  • TypeScript Version: 4.2.4

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:9 (2 by maintainers)

github_iconTop GitHub Comments

28reactions
raitucarpcommented, Jul 9, 2021

@cmditch still complains about BaseEditor, image image

I am getting that same error (without using History). Did you manage to solve it?

solved with dirty trick: type casting, createEditor() as ReactEditor

20reactions
EKOLXcommented, Jun 11, 2021

What to do with this line? const editor = useMemo(() => withHistory(withReact(createEditor())), []); it complains: Argument of type 'BaseEditor' is not assignable to parameter of type 'ReactEditor'. Type 'BaseEditor' is missing the following properties from type 'ReactEditor': insertData, setFragmentData, hasRange

Read more comments on GitHub >

github_iconTop Results From Across the Web

TypeScript Compiling with Visual Studio Code
To generate source maps for your TypeScript files, compile with the --sourcemap option or set the sourceMap property in the tsconfig. json file...
Read more >
Common TypeScript module problems and how to solve them
Solution 1: Locate the correct directory ; "compilerOptions": ; "baseUrl": ; ".", ; "paths": ; "express": ...
Read more >
Typescript project compile errors - node.js - Stack Overflow
When i try to run compile , but on compiling I see the errors that it cannot import express and body parser. But...
Read more >
Getting Started with Executing TypeScript files in ts-node
6. Run the following command to compile your TypeScript code ( main.ts ) without checking for errors ( -T ). Compiling your TypeScript...
Read more >
Getting Started with Typescript - Run & Compile with Webpack
7 of 11 - Getting Started with Typescript - Run & Compile with WebpackLearn how to setup Node.js, ... Your browser can't play...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found