I have this definition:
interface Field {
question: string,
answer: string | string[],
}
but theres error in my text editor if i do this:
if (typeof answer === 'string') {
const notEmpty = answer.trim().length > 0
}
it says string[] doesn't implement trim. is my approach wrong? should i use 2 interfaces? thanks
updated:
my tsconfig.json:
{
"compilerOptions": {
"allowSyntheticDefaultImports": true,
"jsx": "react-native",
"lib": ["dom", "esnext"],
"moduleResolution": "node",
"noEmit": true,
"skipLibCheck": true,
"resolveJsonModule": true,
"strict": true,
"baseUrl": ".",
"paths": {
"*": ["*", "src/*"]
}
}
}
iis of typenumberand the compiler doesn't know how to narrow based on that. Without a minimal reproducible example though I'm not going to go through the effort of demonstrating this. If you want to increase your chances of getting a helpful response, I suggest you follow the guidelines in How to Ask, specifically about providing a minimal reproducible example suitable for dropping into a standalone IDE. Good luck!