2

I would like to be able create (or extend from String) a TypeScript type that behaves like String. To be able to assign value like: const object: MyCustomString = 'some value'

Mamphir
  • 325
  • 2
  • 15
  • 1
    Why do you need this? Can't you just store the string in a field of your object? – Onur Arı Apr 08 '19 at 14:53
  • 1
    It's not possible to directly do what you want to do. You can modify the String prototype but perhaps a better question is what are you actually trying to do because this sounds like [an XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – VLAZ Apr 08 '19 at 14:53
  • I think you are looking for branded types: https://stackoverflow.com/questions/49260143/how-do-you-emulate-nominal-typing-in-typescript/49260286#49260286 – Titian Cernicova-Dragomir Apr 08 '19 at 14:56
  • I ma trying to represent a XML tag of xliff document ` Some text for translation Some translated text ` So the tag `` is mainly string but has some attributes and i want to access it like `TranslationUnit.Source = 'Some text to translate'; TranslationUnit.Source.translate=true;` – Mamphir Apr 08 '19 at 15:09

1 Answers1

3

I quote the documentation on extends:

The extends keyword can be used to subclass custom classes as well as built-in objects.

So, it is authorized to subclass built-in types in ES2015:

class MyString extends String {
    get specialProp() {
        return this + " is special!" 
    }
}

const s = new MyString("abc");
console.log(s.specialProp); // abc is special!

Or, other example:

class SourceString extends String {
    constructor(s: string, public translate = false) {
        super(s);
    }
}

const s = new SourceString("abc", true);
console.log(s.translate); // true

Notice: Instances of your custom class won't be string primitives but String objects:

console.log(typeof s); // "object"
console.log(s instanceof String); // true
Paleo
  • 21,831
  • 4
  • 65
  • 76
  • This is near to what i thought. I found definition of `String` on GitHub [link](https://github.com/Microsoft/TypeScript/blob/2c9f7e6ef1aabc83a25db34adeeaea6da2ddaff6/lib/lib.es5.d.ts#L394) and [link](https://github.com/Microsoft/TypeScript/blob/2c9f7e6ef1aabc83a25db34adeeaea6da2ddaff6/lib/lib.es5.d.ts#L517) \n I would like something like this if its possible, when i try to implement the `StringConstructor {...` i get issues with implementing `(value?: any): boolean;`, gives me an error Type `'SourceString' provides no match for the signature '(value?: any): string'.` – Mamphir Apr 08 '19 at 19:08
  • @JackDaniels I don't understand the error. Could you give your code? – Paleo Apr 08 '19 at 19:25
  • `interface SourceConstructor { new(value?: any): String; (value?: any): string; readonly prototype: String; fromCharCode(...codes: number[]): string; } class SourceString implements SourceConstructor { prototype: string; fromCodePoint(...codes: any[]) { return 'sdfsdf'; } fromCharCode(...codes: number[]){ return 'test'; }; }` – Mamphir Apr 08 '19 at 19:29
  • @JackDaniels I'm sorry I can't help with that. The code you post contains a lot of errors. And I can't understand why it would be needed. Just use the code I suggest. Or write another question. – Paleo Apr 08 '19 at 20:09