簡體   English   中英

打字稿:類型 'Promise<{}>' 不可分配給類型

[英]Typescript: Type 'Promise<{}>' is not assignable to type

我正在使用材料表構建一個表。 以下是如何構建表格的示例: https : //material-table.com/#/docs/features/filtering

我正在嘗試在data傳遞一個數組以填充我的Userlist數組中的字段。

我的componentDidMount()函數中有這個

    pnp.sp.web.lists.getByTitle("Team").items.get().then((items: any[]) => {
        //console.log(items); 
        const UserList = items.map((item) => {

            return {
                UsersName: item.Title,
                UserEmail: item.Email_x0020_Address,
                UserStatus: item.Status,
                UserLocation: item.Location,
                UserAdditional: item.Additional,                        
            }
        });
        //console.log(UserList);

        this.setState({ UserList: [...UserList] });

    });

在我的渲染中,我有這個:

        <MaterialTable
            title=""
            columns={[
            { title: 'Name', field: 'UsersName' },
            { title: 'Email', field: 'UserEmail' },
            { title: 'Status', field: 'UserStatus' },
            { title: 'Location', field: 'UserLocation' },                   
            { title: 'Additional', field: 'UserAdditional' },
            ]}
            data={new Promise((resolve,reject) => {
                (this.state.UserList)
            })}       
            options={{
            filtering: true
            }}
        />

最初我有data={this.state.Userlist}_this.props.data is not a function at MaterialTable錯誤時得到一個_this.props.data is not a function at MaterialTable

所以我改變了,所以我在承諾中傳遞了數據來繞過錯誤,但現在我收到了這個錯誤:

 error TS2322: Type 'Promise<{}>' is not assignable to type '({ UsersName: any; } & { UserEmail: any; } & { UserStatus: any; } & { UserLocation: any; } & { UserAdditional: any; })[] | ((query: Query<{ UsersName: any; } & { UserEmail: any; } & { UserStatus: any; } & { UserLocation: any; } & { ...; }>) => Promise<...>)'.

我對 ReactJS 沒有廣泛的了解,基本上我是 Angular 人,但在搜索時我發現了這個。

執行流程:

反應中的執行順序

方法一:

從componentWillMount方法更改為UNSAFE_componentWillMount因為componentWillMount在2018.More棄用這里https://dev.to/torianne02/componentwillmount-vs-componentdidmount-5f0n

UNSAFE_componentWillMount()
{
 this.setState({ UserList: [] });
  pnp.sp.web.lists.getByTitle("Team").items.get().then((items: any[]) => {
    //console.log(items); 
    const UserList = items.map((item) => {

        return {
            UsersName: item.Title,
            UserEmail: item.Email_x0020_Address,
            UserStatus: item.Status,
            UserLocation: item.Location,
            UserAdditional: item.Additional,                        
        }
    });
    //console.log(UserList);

    this.setState({ UserList: [...UserList] });
});
}

方法二:在構造函數中初始化狀態:

constructor() {
    // Required step: always call the parent class' constructor
    super(props);

    // Set the state directly. 
    this.state = {
        UserList: [],
        //if you have any other state variables add here
    }
  }

在 componentDidMount 中調用 get

componentDidMount(){
    pnp.sp.web.lists.getByTitle("Team").items.get().then((items: any[]) => {
        //console.log(items); 
        const UserList = items.map((item) => {

            return {
                UsersName: item.Title,
                UserEmail: item.Email_x0020_Address,
                UserStatus: item.Status,
                UserLocation: item.Location,
                UserAdditional: item.Additional,                        
            }
        });
        //console.log(UserList);

        this.setState({ UserList: [...UserList] });
    })
}

兩種情況下的渲染功能

render(){
   return ( <MaterialTable
   title=""
   columns={[
   { title: 'Name', field: 'UsersName' },
   { title: 'Email', field: 'UserEmail' },
   { title: 'Status', field: 'UserStatus' },
   { title: 'Location', field: 'UserLocation' },                   
   { title: 'Additional', field: 'UserAdditional' },
   ]}
   data={this.state.Userlist}      
   options={{
   filtering: true
   }}
/>)
}

TypeScript型'承諾<void | object> ' 不可分配給類型 'Promise<object> ' 在 Promise.then()<div id="text_translate"><p> 我目前正在嘗試以 base64 格式實現緩存文檔的服務。 我希望這個服務從 sessionStorage 中獲取文檔,如果 sessionStorage 中沒有文檔,則從 IRequestService 中獲取它,然后將其保存到 sessionStorage。 我嘗試了這種方法,但出現類型錯誤</p><pre>Type 'Promise&lt;void | GetDocumentResult&gt;' is not assignable to type 'Promise&lt;GetDocumentResult&gt;'.</pre><p> 代碼如下所示:</p><pre> getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise&lt;GetDocumentResult&gt; { if (this.storageKey in sessionStorage) { const documentsMap: Map&lt;DocumentID, GetDocumentResult&gt; = new Map(JSON.parse(sessionStorage.getItem(this.storageKey).)) if (documentsMap.get(documentId).== undefined) { return new Promise(resolve =&gt; resolve(documentsMap,get(documentId).)) } } return requestService.getDocument(requestId, documentId) .then(value =&gt; {this.setDocument(documentId, value)}) }</pre><p> 我期待 Promise&lt;GetDocumentResult&gt;.then 的返回類型是 Promise&lt;GetDocumentResult&gt;,但由於某種我不明白的原因,它是 Promise&lt;void | 獲取文檔結果&gt;</p><p> 有人知道為什么會這樣嗎?</p><p> 謝謝</p></div></object></void>

[英]TypeScript Type 'Promise<void | Object>' is not assignable to type 'Promise<Object>' on Promise.then()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

相關問題 打字稿:類型&#39;Promise &lt;{}&gt;&#39;不能分配給&#39;Promise&#39; <void> “ 打字稿:輸入“承諾”<void> &#39; 不可分配給類型 &#39;void | 破壞者 Promise 參數類型不可分配 TypeScript型'承諾<void | object> ' 不可分配給類型 'Promise<object> ' 在 Promise.then()<div id="text_translate"><p> 我目前正在嘗試以 base64 格式實現緩存文檔的服務。 我希望這個服務從 sessionStorage 中獲取文檔,如果 sessionStorage 中沒有文檔,則從 IRequestService 中獲取它,然后將其保存到 sessionStorage。 我嘗試了這種方法,但出現類型錯誤</p><pre>Type 'Promise&lt;void | GetDocumentResult&gt;' is not assignable to type 'Promise&lt;GetDocumentResult&gt;'.</pre><p> 代碼如下所示:</p><pre> getDocument(requestId: string, documentId: DocumentID, requestService: IRequestService): Promise&lt;GetDocumentResult&gt; { if (this.storageKey in sessionStorage) { const documentsMap: Map&lt;DocumentID, GetDocumentResult&gt; = new Map(JSON.parse(sessionStorage.getItem(this.storageKey).)) if (documentsMap.get(documentId).== undefined) { return new Promise(resolve =&gt; resolve(documentsMap,get(documentId).)) } } return requestService.getDocument(requestId, documentId) .then(value =&gt; {this.setDocument(documentId, value)}) }</pre><p> 我期待 Promise&lt;GetDocumentResult&gt;.then 的返回類型是 Promise&lt;GetDocumentResult&gt;,但由於某種我不明白的原因,它是 Promise&lt;void | 獲取文檔結果&gt;</p><p> 有人知道為什么會這樣嗎?</p><p> 謝謝</p></div></object></void> Typescript:字符串可分配給 {} 類型 打字稿:不可分配給鍵入錯誤 TypeScript:類型參數不可分配 打字稿-無法分配給打字生 Typescript 類型“字符串”不可分配給類型 Typescript 錯誤:鍵入此不能分配給鍵入此
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM