Skip to content

Instantly share code, notes, and snippets.

@uzimith
Created October 12, 2017 03:04
Show Gist options
  • Save uzimith/326ee2140e7f84d6b088c74255cd0461 to your computer and use it in GitHub Desktop.
Save uzimith/326ee2140e7f84d6b088c74255cd0461 to your computer and use it in GitHub Desktop.
Object.assignエラー
/* @flow */
type Article = {
id: string
}
type State = {
articles: Article[]
}
const state: State = {
articles: [{ id: 'fuga' }]
}
const newArticle = [
{
id: 'hoge'
}
]
const article: Article[] = Object.assign([], state.articles, newArticle)
console.log(article)
// 31: const article: Array<Article> = Object.assign({}, state.articles, newArticle)
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ call of method `assign`. Expected object instead of
// 31: const article: Array<Article> = Object.assign({}, state.articles, newArticle)
// ^^^^^^^^^^^^^^ array type
interface Article {
id: string
}
interface State {
articles: Article[]
}
const state: State = {
articles: [{ id: 'fuga' }]
}
const newArticle = [
{
id: 'hoge'
}
]
const article: Article[] = Object.assign({}, state.articles, newArticle)
console.log(article)
// {"0":{"id":"hoge"}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment