繁体   English   中英

Typescript ts2322:类型“{ id: number, name: string, paymentStatus: string }”不可分配给类型

[英]Typescript ts2322: Type '{ id: number, name: string, paymentStatus: string }' is not assignable to type

嗨,我刚开始使用 React 学习 Typescript,我需要帮助,我无法弄清楚这个错误:

"Type '{ id: number, name: string, paymentStatus: string }' is not assignable to type 'InvoiceType'. 
Object literal may only specify known properties but 'paymentStatus' does not exist in 'InvoiceType'. Did you mean to write 'payment_status'?"

如果所有组件以及接口、类型和数据都在一个文件中,它可以正常工作,但是在将它们分开后我得到了错误。 我试过这个payment_status?: 'paid' | '待办的'; InvoiceType 类型中,但我得到了同样的错误。

这是我的界面

export interface IinvoiceListProps {
    invoiceData: InvoiceDataType
}

export type InvoiceDataType = {
    customer_name: string;
    invoices: Array<InvoiceType>
}

export type InvoiceType = {
    id: number;
    name: string;
    total: string;
    payment_status?: PaymentStatus;
}

export type PaymentStatus = 'paid' | 'pending';

这是我的应用

import React from 'react';
import InvoiceList from './components/InvoiceList';
import data from './data';

function App() {
  return (
    <div className="App">
     <InvoiceList invoiceData={data} />
    </div>
  );
}

export default App;

这是发票清单

import { IinvoiceListProps } from "../interfaces_types";

const InvoiceList = ({invoiceData:{customer_name,invoices}}:IinvoiceListProps)=>{
    return (
        <div>
            <h1>{customer_name}</h1> 
            <ul>
                {
                    invoices.map((invoice)=>(
                        <li key={invoice.id}>
                            <h3>{invoice.name}</h3>
                            <p>{invoice.total} = {invoice?.payment_status}</p>
                        </li>
                    ))
                }
            </ul>
        </div>
    )
}

export default InvoiceList;

而且数据

import { InvoiceDataType } from "../interfaces_types";

const data: InvoiceDataType = {
    customer_name:'Test',
    invoices: [
        {
            id: 1,
            name: 'Design Work',
            total: '40.00'
        },
        {
            id: 2,
            name: 'SEO Work',
            total: '50.00'
        },
        {
            id: 3,
            name: 'Dev Work',
            total: '70.00',
            paymentStatus: 'pending'
        },
    ]
}

export default data;

您的界面已归档名称为payment_status 但是您正在尝试分配paymentStatus 将数据中的paymentStatus更改为payment_status

{
    id: 3,
    name: 'Dev Work',
    total: '70.00',
    payment_status: 'pending'
 }

TS2322:输入“NotRequiredArraySchema”<string | undefined> ' 不可分配给类型 'Ref | 架构<string[] | using formik + yup validator< div><div id="text_translate"><p> 我正在解决这个问题。 我正在使用 yup 进行 formik 验证。 方案代码:</p><pre> const schema = yup.object().shape&lt;Partial&lt;NoteInterface&gt;&gt;({ noteName: yup.string().required().max(32), groupId: yup.number().required(), noteText: yup.string().required().max(32), noteDuration: yup.string().required().max(32), noteTags: yup.array().of(yup.string()), });</pre><p> 和 model 代码在这里:</p><pre> export enum cNote { noteId = 'noteId', groupId = 'groupId', noteName = 'noteName', noteText = 'noteText', noteDuration = 'noteDuration', noteCreation = 'noteCreation', noteTags = 'noteTags', } export interface NoteInterface { noteId: string; groupId: number; noteName: string; noteText: string; noteDuration: string; noteCreation: string; noteTags: string[]; } export const Note = (): NoteInterface =&gt; ({ noteId: '', groupId: 0, noteName: '', noteText: '', noteDuration: '', noteCreation: '', noteTags: [], });</pre><p> 一切正常,除了有这个问题的noteTags:</p><pre> TS2322: Type 'NotRequiredArraySchema&lt;string | undefined&gt;' is not assignable to type 'Ref | Schema&lt;string[] | undefined&gt; | MixedSchema&lt;string[] | undefined&gt; | undefined'. Type 'NotRequiredArraySchema&lt;string | undefined&gt;' is not assignable to type 'MixedSchema&lt;string[] | undefined&gt;'.</pre><p> 感谢您的时间</p></div></string[]></string>

[英]TS2322: Type 'NotRequiredArraySchema<string | undefined>' is not assignable to type 'Ref | Schema<string[] | Using Formik + yup validator

暂无
暂无

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

相关问题 TypeScript 错误 TS2322:类型“选项”不可分配给类型“(字符串和选项)| (号码和选项)| (只读字符串 [] &amp; 选项)' React TypeScript 错误:TS2322 - 类型''不可分配给类型'IntrinsicAttributes &amp; String' 类型“数字”不可分配给类型“字符串”.ts(2322) 输入&#39;{时间:数字; }&#39; 不可分配给类型 &#39;number&#39;。 TS2322 useRef() 导致 TS2322:类型“数字”不可分配给类型“未定义” TS React 数据导入:“TS2322:类型 &#39;{ data: { key1: string; }; }&#39; 不可分配给类型 &#39;IntrinsicAttributes &amp; Props&#39;。” TS2322:类型'{文本:字符串; }' 不可分配给类型“字符串”。 在传递道具时 TS2322:输入“NotRequiredArraySchema”<string | undefined> ' 不可分配给类型 'Ref | 架构<string[] | using formik + yup validator< div><div id="text_translate"><p> 我正在解决这个问题。 我正在使用 yup 进行 formik 验证。 方案代码:</p><pre> const schema = yup.object().shape&lt;Partial&lt;NoteInterface&gt;&gt;({ noteName: yup.string().required().max(32), groupId: yup.number().required(), noteText: yup.string().required().max(32), noteDuration: yup.string().required().max(32), noteTags: yup.array().of(yup.string()), });</pre><p> 和 model 代码在这里:</p><pre> export enum cNote { noteId = 'noteId', groupId = 'groupId', noteName = 'noteName', noteText = 'noteText', noteDuration = 'noteDuration', noteCreation = 'noteCreation', noteTags = 'noteTags', } export interface NoteInterface { noteId: string; groupId: number; noteName: string; noteText: string; noteDuration: string; noteCreation: string; noteTags: string[]; } export const Note = (): NoteInterface =&gt; ({ noteId: '', groupId: 0, noteName: '', noteText: '', noteDuration: '', noteCreation: '', noteTags: [], });</pre><p> 一切正常,除了有这个问题的noteTags:</p><pre> TS2322: Type 'NotRequiredArraySchema&lt;string | undefined&gt;' is not assignable to type 'Ref | Schema&lt;string[] | undefined&gt; | MixedSchema&lt;string[] | undefined&gt; | undefined'. Type 'NotRequiredArraySchema&lt;string | undefined&gt;' is not assignable to type 'MixedSchema&lt;string[] | undefined&gt;'.</pre><p> 感谢您的时间</p></div></string[]></string> 奇怪的 TS2322(类型不可分配)错误 类型“[Images]”不可分配给类型“string”.ts(2322)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM