Choose Typescript type/interface conditionally on external condition

Viewed 42

I would like to choose a TS interface or type based on an external condition other that what is being typed.

The external condition could be a feature toggle for example.

I will try to explain my point:

type NotNullableName = string;
type NullableName = string | null;

const getNotNullableName = (name: NotNullableName) => name
const getNullableName = (name: NullableName) => name

At the moment I have to do:

const getName =  externalCondition ? getNotNullableName :  getNullableName;

But I would like to achieve something like this:

const getName = (name: externalCondition ? NotNullableName : NullableName) => name

Is there anyway I could achieve that?

I am providing a TS playground to make my point clear

1 Answers
Related