When using type unions, you have to cast the complex types to use their properties:
const data = {
documentId: (action.document as Document)?._id
}
If you want to store a string
for document in your interface for Action
, I would make an additional optional property to save you from having to cast document
.:
export interface Action {
document?: Document,
docString?: string
}
Be aware that document
is a reserved word in javascript. I'd use doc
or something else.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…