refactor ternary operation and typeof