I want to have a type which looks like:
type MyProps = {
context: string;
// other props
}
Except I want the context
to have been generated from a specific function / or at least have a specific type (though it’s really a string). For example:
function getContext(props: ContextProps) {
const { a, b, c } = props;
return `${a} [ ${b} / ${c} ]`;
}
const myProps = {
context: getContext({ a: "test", b: "ABC", c: "123" }),
};
But setting context
to any string should fail:
const myProps = {
context: "some other string",
};
I’ve tried this, but it allows a plain string to be set to context
:
function getContext(props: ContextProps) {
const { a, b, c } = props;
return `${a} [ ${b} / ${c} ]` as ContextString;
}
type ContextString = string;
// or
interface ContextString extends String { }
Is it possible to enforce this?