Only with "OpaqueTypes" or "Nominal Types" aslong as you have an implementation that can make a string a HexCode the implementation is as follows.
export type Opaque<K, T> = T & { __TYPE__: K };
type HexCode = Opaque<string, "HexCode">
const createHexCode = (str: string): HexCode => {
// implementation that forces string to be hexCode
return str.toString() as HexCode // casting is needed.
}
const test = createHexCode("#333");
const isAssignableString: string = test; // yes anything that is HexCode is still technically a string.
const isAssignableHexCode: HexCode = "standard string" // error
this pattern is also useful for things like PositiveInteger and PasswordHash where you don't want anyone to accidentally assign a plain-string to that value you want to force people to use a function that returns a PasswordHash