You have this error because you use a variable : i
to go throught your object.
playground
interface Field {
question: string,
answer: string | string[],
}
const answerArray: Field['answer'][] = ['tes'];
const i = 0;
if (typeof answerArray[i] === 'string') {
const notEmpty = answerArray[i].trim().length > 0
}
Between the two line :
if (typeof answerArray[i] === 'string') {
const notEmpty = answerArray[i].trim().length > 0
TypeScript consider that answerArray[i]
could result in different values.
The soluce is to store the value inside of a pointer, like :
playground
interface Field {
question: string,
answer: string | string[],
}
const answerArray: Field['answer'][] = ['tes'];
const i = 0;
const myAnswer = answerArray[i];
if (typeof myAnswer === 'string') {
const notEmpty = myAnswer.trim().length > 0
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…