Однако TypeScript определяет только тип объекта и полностью игнорирует значение NULL в возвращаемом типе.
Вот моя функция:
Код: Выделить всё
import type { Voucher } from '@/api/vouchers/index.types';
export const getStepperData = (steps: Voucher['steps']) => {
if (!steps || steps.length === 0) {
return null;
}
return {
labels: steps.map((step) => step.title),
activeStep: steps.findLastIndex((step) => step.is_active) + 1,
};
};
Код: Выделить всё
function getStepperData(
steps: Voucher["steps"],
): { labels: string[]; activeStep: number }
Моя версия TypeScript:
Код: Выделить всё
"typescript": "^5.5.2"
Код: Выделить всё
{
"compilerOptions": {
"jsx": "react-jsx",
"removeComments": true,
"sourceMap": true,
"baseUrl": ".",
"module": "ESNext",
"moduleResolution": "bundler",
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"forceConsistentCasingInFileNames": true,
"strict": false,
"noEmit": true,
"allowJs": true,
"checkJs": false,
"lib": ["DOM", "ESNext", "WebWorker"],
"paths": { "@/*": ["./src/*"] },
"isolatedModules": true,
"skipLibCheck": true
}
}
Подробнее здесь: https://stackoverflow.com/questions/798 ... eturn-type
Мобильная версия