my question is can you create a method to a generic union type in typescript ?
below is an illustration of what I would like to achieve with it
type Maybe<T> = T | Error
const ok_one:Maybe<string> = 'just something'
const not_ok_one:Maybe<string> = new Error('nothing')
ok_one.bind((word:string)=>(word+' more'))
// would give 'just something more'
not_ok_one.bind((word:string)=>(word+' more'))
// stay the same
I found how to make method for simple types Add method to string class that one pretty close, but I cannot really adapt it to my problem Typescript: create extension method for type alias and How to extend a primitive type in typescript?
sounds like it would be easier to do it with a class but I wanted to try with type alias.
Is that even possible ?
I have the feeling I am being naive and that I am missing important clues about about prototype and class/interfaces here that prevent me to figure out.
edit reformulation according to comments removed update and put it as an answer