I have encountered this scenario:
function createCar(name: string, callback: () => void)
function buildEngine(name: string): Engine
function createCarWithEngine(carName: string, engineName: string, callback: (param: Engine) => void) {
let createdEngine = createdEngines.find((engine) => engine.name === engineName)
if (!createdEngine) createdEngine = buildEngine(engineName)
createCar(carName, () => callback(createdEngine)) // encountering an error here
}
While working in VSCode, it indicates that createdEngine
might be undefined. However, the following code seems to work without any issues:
const fn = callback(createdEngine)
createCar(carname, () => fn)
Can anyone explain if this behavior is expected?