type returnBetweenType = { isBetween: true; Pos: { from: string; to: string; }; }; type returnCurrentType = { isBetween: false; Pos: { Pos: string; }; }; export type trainDataType = { Pos?: string; Direction?: number; Index?: number; Line?: string; PosNum?: number; Type?: string; delay?: string | number; num?: string; }; type trainData = ( trainData: trainDataType ) => returnBetweenType | returnCurrentType; export const trainPosition: trainData = (trainData) => { if (!trainData?.Pos) return { isBetween: false, Pos: { Pos: "" } }; const { Pos, Direction } = trainData; if (Pos.match("~")) { const [topST, downST] = Pos.replace("(下り)", "") .replace("(上り)", "") .split("~"); const from = Direction == 1 ? topST : downST; const to = Direction == 1 ? downST : topST; return { isBetween: true, Pos: { from, to } }; } else return { isBetween: false, Pos: { Pos } }; };