0

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

fulverin
  • 53
  • 6
  • 1
    This is unfortunately broken in so many ways that I think it probably needs to be [edit]ed to be more focused by removing some of the issues not directly related to your question. ① Type names are conventionally written in UpperPascalCase and it is distracting to do otherwise; `maybe` should be `Maybe`. ② If `Maybe`is just a type then it is [erased](//www.typescriptlang.org/docs/handbook/2/basic-types.html#erased-types) along with the rest of the TS type system when compiled to JS; there is no `Maybe.prototype` at runtime to use. – jcalz Apr 28 '23 at 13:18
  • 1
    ③ Even if there were one, it wouldn't have any effect on arbitrary primitives; `ok_one` is just a string at runtime, it has no `if_error_change` method. ④ You *could* modify `Object.prototype`, but that's widely considered bad practice and even that wouldn't work for `Maybe` or `Maybe`. – jcalz Apr 28 '23 at 13:18
  • 1
    ⑤ JS doesn't have pass-by-reference so `this = ...` won't affect anything ⑥ `typeof this` will never be `"Error"` because that's the runtime `typeof` operator; at best it would be `object`. ⊛ I am not sure which of those points to expand into an answer. Please consider editing the question to remove all but one or maybe two of those issues. – jcalz Apr 28 '23 at 13:21
  • thank you t@jcalz his is what I wanted to know, my guts were telling me many things were in my way but needed someone to nail it. I edit my question is a more reasonably working sample – fulverin Apr 28 '23 at 14:38
  • I'm confused now by the update. ① **what is your actual question?** It should be written explicitly and clearly so that it can be answered or pointed at another question with a relevant answer. ② Please edit the post so that it reads like a single coherent question and not like a series of updates; if the update is meant as an answer it should be posted separately as an answer; if it's not it should be folded in. ③ What does "cincture" mean in this context? Is that translated from another language? If so you might want to look up more conventional English terms so as not to be distracting. – jcalz Apr 28 '23 at 14:55
  • 1/ so the actual question is : is there a way to attach a method to a generic union type. my understanding is "no". 2/ assuming it cannot work I shared what could be the closest working code (but I am not sure how relevant it is, folding it is a good idea indeed) 3/ well sounds like functional programing have specific terms to handle errors like this in doubt I picked a "close enough word" because I did not want to use the wrong term by accident. I guess cincture is old English word you only find on Wiktionary. it means catch , like caching an error, but I only want to update if it exist – fulverin Apr 28 '23 at 15:47
  • I'm hoping you can take my advice from my previous comments and edit your question accordingly. If not then I think we're probably at an impasse now. The answer to that question is still "no, types are erased when compiled to JS, so there's nothing to attach a method to". But I won't write anything up yet because of the issues with the question that ought to be resolved first. – jcalz Apr 28 '23 at 15:52
  • as far as I am concerned you perfectly replied to all my interrogations, thank you again. I am not sure what is the policy , should I let you write no as an answer an I validate it or I should do it myself and paste what work around code that works for me in case someone would find it useful ? – fulverin Apr 28 '23 at 16:07

0 Answers0