简体   繁体   English

SlateJS Typscript node.children url 和 type 属性不存在

[英]SlateJS Typscript node.children url and type property does not exist

TLDR: Error message for SlateJS serializing to HTML Property 'children' does not exist on type 'Node[]' . TLDR:SlateJS 序列化为 HTML Property 'children' does not exist on type 'Node[]'错误消息Property 'children' does not exist on type 'Node[]'

Going off on SlateJS Serializing Docs but in tsx.开始使用SlateJS 序列化文档,但在 tsx 中。

import React, { useCallback, useMemo, useState } from "react";
import escapeHtml from 'escape-html';
import { Editor, createEditor, Node, Text } from 'slate';
import { withHistory } from 'slate-history';

const serializeHTML = (node: Node[]) => {
  if (Text.isText(node)) {
    return escapeHtml(node.text)
  };

  const children = node.children.map((n: Node[]) => serializeHTML(n)).join('');

  switch (node.type) {
    case 'link':
      return `<a href="${escapeHtml(node.url)}">${children}</a>`
    case 'list-item':
      return ``
    case 'paragraph':
      return `<p>${children}</p>`
    case 'quote':
      return `<blockquote>${children}</blockquote>`
    default:
      return children
  };
};

I'm getting the following property error for children , type and url .对于childrentypeurl ,我收到以下属性错误。

  • Property 'children' does not exist on type 'Node[]'
  • Property 'type' does not exist on type 'Node[]'
  • Property 'url' does not exist on type 'Node[]'

In my rich editor I have:在我的丰富编辑器中,我有:

const RichTextEditor = (props: RichTextEditorProps) => {
    const editor = useMemo(() => withImages(withHistory(withReact(createEditor()))), []);
  const [value, setValue] = useState<Node[]>(initialValue);
  const html = serializeHTML(value);
      
  return (
    <Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
       ...
    </Slate>
  )

I already have the type dependencies.我已经有了类型依赖。

  • "@types/slate-react": "^0.22.9"
  • "@types/slate": "^0.47.7",

serializeHTML() method can just take a single Node and have the iterator from where it's called. serializeHTML()方法可以只接受一个 Node 并从它被调用的地方获得迭代器。

Function :功能

const serializeHTML = (node: Node) => {
  ...

  const children = node.children.map((n: Node) => serializeHTML(n)).join('');
  ...
};

Call :电话

const RichTextEditor = (props: RichTextEditorProps) => {
    const editor = useMemo(() => withImages(withHistory(withReact(createEditor()))), []);
  const [value, setValue] = useState<Node[]>(initialValue);
  
  const html = value.map(v => serializeHTML(v)).join('');
      
  return (
    <Slate editor={editor} value={value} onChange={newValue => setValue(newValue)}>
       ...
    </Slate>
  )
};

It looks like you are trying to access properties on a Node Array instead of a Node.看起来您正在尝试访问节点阵列而不是节点上的属性。

A couple of other things worth mentioning:还有一些值得一提的事情:

  1. Later versions of Slate (.5x) are written in TypeScript and should not require explicit types installation.更高版本的 Slate (.5x) 是用 TypeScript 编写的,不需要显式安装类型。 Those @types/xxx dependencies you listed are not the right types.您列出的那些 @types/xxx 依赖项不是正确的类型。 They are for .47x.它们适用于 0.47 倍。
  2. Be sure to import the Node type from Slate ( import { Node } from 'slate' ).确保从 Slate 导入Node类型( import { Node } from 'slate' )。 There is a globally available Node type that will be used if you don't and it is not the one you want (this doesn't seem to be your issue but worth mentioning for anyone else who runs into similar errors)有一个全局可用的Node类型,如果你不这样做,它将被使用,它不是你想要的(这似乎不是你的问题,但对于遇到类似错误的其他人来说值得一提)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型“{}”上不存在属性“children” - Property 'children' does not exist on type '{}' 打字稿:类型“字符串”上不存在属性“值”| { 值:字符串; 标签:字符串; } - Typscript: Property 'value' does not exist on type 'string | { value: string; label: string; } 类型 '{ children?: ReactNode; 上不存在属性 'onClick' }' - Property 'onClick' does not exist on type '{ children?: ReactNode; }' 属性不存在于类型 'IntrinsicAttributes & { children?: ReactNode; }' - Property does not exist on type 'IntrinsicAttributes & { children?: ReactNode; }' "<i>TypeScript error: Property &#39;children&#39; does not exist on type &#39;{ children?: ReactNode;<\/i> TypeScript 错误:类型 &#39;{ children?: ReactNode; 上不存在属性 &#39;children&#39;<\/b> <i>}&#39;<\/i> }&#39;<\/b>" - TypeScript error: Property 'children' does not exist on type '{ children?: ReactNode; }' 属性 &#39;XYZ&#39; 不存在于类型 &#39;Readonly&lt;{ children?: ReactNode; }&gt; 和只读&lt;{}&gt;&#39; - Property 'XYZ' does not exist on type 'Readonly<{ children?: ReactNode; }> & Readonly<{}>' React Antd 模态属性 &#39;children&#39; 在类型&#39;IntrinsicAttributes &amp; ModalProps&#39; 上不存在 - React Antd Modal Property 'children' does not exist on type 'IntrinsicAttributes & ModalProps' 类型'IntrinsicAttributes &amp; PhoneInputProps &amp; { children?: ReactNode; 上不存在属性'ref' } - Property 'ref' does not exist on type 'IntrinsicAttributes & PhoneInputProps & { children?: ReactNode; } React:如何解决:“IntrinsicAttributes & Props”类型上不存在属性“children” - React: How to solve: Property 'children' does not exist on type 'IntrinsicAttributes & Props' TypeScript 错误:属性“children”在类型“ReactNode”上不存在 - TypeScript error: Property 'children' does not exist on type 'ReactNode'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM