I am trying to create a state variable named hidden
to be a dictionary (ex: [{'cutomkey1':'somevalue', 'customkey2':'somevalue'}]
). hidden can be empty [{}]
.
In one of my methods I want to push an item {'asd':'asd'}
to the hidden state variable and add them up.
I keep getiting this error:
Type 'String[] | undefined' is not an array type.
I'm new to typescript and I am not sure i set all the variables correctly. this is my code (only the relevant parts):
import React from 'react';
export type AppState = {
tickets?: Ticket[],
hidden?: Array<String>,
search: string;
}
export class App extends React.PureComponent<{}, AppState> {
state: AppState = {
search: '',
hidden: []
}
hideCard = (e: React.SyntheticEvent<EventTarget>) => {
let targ = e.target as HTMLElement
let parent = targ.parentElement as HTMLElement
let dict: Map<string, string> = new Map();
dict.set(parent.id, parent.id)
this.setState({
hidden: [...this.state.hidden, {dict}]
})
console.log(this.state.hidden)
parent.classList.add('hideTkt')
}
}
export default App;
new code:
export type AppState = {
tickets?: Ticket[],
hidden?: Array<String> | undefined,
search: string;
}
export class App extends React.PureComponent<{}, AppState> {
state: AppState = {
search: '',
hidden: []
}
hideCard = (e: React.SyntheticEvent<EventTarget>) => {
let targ = e.target as HTMLElement
let parent = targ.parentElement as HTMLElement
let dict: Map<string, string> = new Map();
dict.set(parent.id, parent.id)
if (this.state.hidden) {
this.setState({
hidden: [...this.state.hidden, {dict}]
})
}
console.log(this.state.hidden)
parent.classList.add('hideTkt')
}
this is the error:
Type '(String | { dict: Map<string, string>; })[]' is not assignable to type 'String[]'. Type 'String | { dict: Map<string, string>; }' is not assignable to type 'String'. Type '{ dict: Map<string, string>; }' is not assignable to type 'String'. Object literal may only specify known properties, and 'dict' does not exist in type 'String'.
hidden: Array<String>,
– JdType '(String | { dict: Map<string, string>; })[]' is not assignable to type 'String[]'. Type 'String | { dict: Map<string, string>; }' is not assignable to type 'String'. Type '{ dict: Map<string, string>; }' is not assignable to type 'String'. Object literal may only specify known properties, and 'dict' does not exist in type 'String'.
and as said, hidden can be empty, shouldn't this be there ? – Hewhidden: Array<Record<string, string>>
– Jd