I struggle with a field typed string | string[]
.
I assign an array to it but if I want to push a string to it i get a type error.
For illustration see this fragment:
let col: AgGridColumnProps = {
...
type: [field.type.toLowerCase()]
}
for (const [ending, tmp] of Object.entries(nameEndingMap)) {
if (theName.endsWith(ending)) {
const typ: string = tmp[0]
...
col.type.push(typ)
}
}
Results in this error:
Property 'push' does not exist on type 'string | string[]'.
Property 'push' does not exist on type 'string'.
The definition of type
from Ag-Grid is type?: string | string[]
.
How can I make typescript understand, that push
ing to type
is valid here?
Edit: While
let col: AgGridColumnProps = {
cellClass: type2classMap[field.type.toLowerCase()],
type: [field.type.toLowerCase()]
}
does not compile due to thye error mentioned above,
let col: AgGridColumnProps = {}
col.cellClass = type2classMap[field.type.toLowerCase()]
col.type = [field.type.toLowerCase()]
compiles fine. I'm still dumbfolded what is happening there. I also was unable to reproduce this issue on a minimal example as suggested by Mike S.
question from:
https://stackoverflow.com/questions/65882747/how-to-push-to-something-typed-string-string 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…