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.

types error in 0.58.3 when i use typescript

See original GitHub issue

Do you want to request a feature or report a bug?

bug

What’s the current behavior?

i got error:

Argument of type 'Node[]' is not assignable to parameter of type 'SetStateAction<{ type: string; children: { text: string; }[]; }[]>'.
  Type 'Node[]' is not assignable to type '{ type: string; children: { text: string; }[]; }[]'.
    Type 'Node' is not assignable to type '{ type: string; children: { text: string; }[]; }'.
      Property 'type' is missing in type 'Editor' but required in type '{ type: string; children: { text: string; }[]; }'.  TS2345

Slate: 0.58.3 Browser: Chrome
OS: Mac

What’s the expected behavior?

no error

this is my code:

// code from https://docs.slatejs.org/walkthroughs/01-installing-slate
// Import React dependencies.
import React, { useEffect, useMemo, useState } from "react";
// Import the Slate editor factory.
import { createEditor } from 'slate'

// Import the Slate components and React plugin.
import { Slate, Editable, withReact } from 'slate-react'


export const Editor = () => {
    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>
    )
  }

this is my way , can fix the error:

change the code: 1.add import { Node } from 'slate'; 2. change

const [value, setValue] = useState([
      {
        type: 'paragraph',
        children: [{ text: 'A line of text in a paragraph.' }],
      },
    ])

to

const [value, setValue] = useState<Node[]>([
      {
        type: 'paragraph',
        children: [{ text: 'A line of text in a paragraph.' }],
      },
    ])

its work!!!

the point is useState<Node[]>

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:16
  • Comments:12 (1 by maintainers)

github_iconTop GitHub Comments

22reactions
codercodingthecodecommented, Apr 2, 2021

This is how I solved it.

 import { Descendant } from 'slate'
 const [value, setValue] = useState<Descendant[]>([{ type: 'paragraph', children: [{ text: '' }] }])

The documentation doesn’t show this part, but you also have to add slate history and empty children. Basically, the whole sample app looks like this:

import React, { useMemo, useState } from 'react'
import { createEditor, BaseEditor, Descendant } from 'slate'
import { Slate, Editable, withReact, ReactEditor } from 'slate-react'
import { HistoryEditor } from 'slate-history'

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

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

export const Note = () => {
    const editor = useMemo(() => withReact(createEditor()), [])
    const [value, setValue] = useState<Descendant[]>([{ type: 'paragraph', children: [{ text: '' }] }])
    return (
        <Slate
            editor={editor}
            value={value}
            onChange={(newValue) => setValue(newValue)}
        >
            <Editable />
        </Slate>
    )
}
0reactions
codercodingthecodecommented, Jul 13, 2021

@jsoneaday and @connorjacobsen or anyone else. If you guys can share a sandbox I can take a look at it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error: 'types' can only be used in a .ts file - Visual Studio Code ...
I'm using flow with vscode but had the same problem. I solved it with these steps: Install the extension Flow Language Support.
Read more >
TypeScript errors and how to fix them
Common Errors​​ Below you find a list of common TypeScript errors along with the buggy code and its fixed version.
Read more >
Documentation - TypeScript 4.6
TypeScript now shows JavaScript syntax and binding errors in your file, such as using incorrect modifiers, duplicate declarations, and more. These will ...
Read more >
Typescript errors when using @polkadot/api
As per the linked SO answer - metadata describes the chain, these types are only available at runtime, each chain is vastly different,...
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