1

This is mainly a question on how to add/extend any existing type with static custom methods.

I want to have the String prototype extended with a function, e.g. isNullOrEmpty which should be invoked the C# way:

if(!String.isNullOrEmpty(myStringToCheck)){
    // do things as of myStringToCheck is set to something
}

In plain javascript I could do something like

String.isNullOrEmpty = function (s) {
    if (s == null || s === "")
        return true;

    return false;
}

But, when calling it inside a TypeScript it tells me

The property 'isNullOrEmpty' does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'.

How can this be done so it is known by TypeScript?

Edit #1

How is String.fromCharCode() implemented which is already known by TypeScript?

Edit #2

Because of other dependencies in the project I'm currently only allowed to use TypeScript 1.0!

Edit #3

String.d.ts

interface StringConstructor {
    isNullOrEmpty(): boolean;
}

interface String {
    format(...args: any[]): string;
    isNullOrEmpty(): boolean;
} 

and my String.ts

/// <reference path="../../../typings/String.d.ts"/>

String.prototype.format = function (): string {
    var formatted = this;
    for (var i = 0; i < arguments.length; i++) {
        var regexp = new RegExp("\\{" + i + "\\}", "gi");
        formatted = formatted.replace(regexp, arguments[i]);
    }
    return formatted;
}

String.isNullOrEmpty = function(s) { <-- here is the exception
    if (s == null || s === "")
        return true;
    return false;
}

Solution #1 (TypeScript version > 1.0?)

see first answer by MartyIX

Solution #2 (workaround for TypeScript version 1.0)

see second answer

1
  • You should mark your answer with the green tick. :) Commented Oct 23, 2015 at 6:28

2 Answers 2

2
// String.ts
interface StringConstructor {
    isNullOrEmpty(text: string): boolean;
}

interface String {
    isNullOrEmpty(text: string): boolean;
}

String.isNullOrEmpty = (s:string):boolean => {
    return (s == null || s === "");
};

// OtherFile.ts
///<reference path="path/to/String.ts" />

if(!String.isNullOrEmpty("test")){
    // Do something
}

var isEmpty = String.fromCharCode(100).isNullOrEmpty("Nah");

This works for me. The interface extends StringConstructor.

Sign up to request clarification or add additional context in comments.

11 Comments

And how would I implement the method? I have a String.ts where I have some more extensions already implemented (but currently only new functions instead of static functions).
Please see the edited answer. You don't need to do anything fancy here. Just do what you would do in plain JavaScript.
This is not working. Is still says The property 'isNullOrEmpty' does not exist on value of type '{ prototype: String; fromCharCode(...codes: number[]): string; (value?: any): string; new(value?: any): String; }'. Maybe an version-issue?
What about the latest version?
Well, if you my code to typescriptlang.org/Playground, it works. Unfortunately, I don't have TypeScript 1.0, it's rather old.
|
1

As by comments from @MartyIX, it seems to be a problem with TypeScript 1.0. As a workaround I've added a method to the prototype of String:

String.d.ts (definition)

interface String {
    isNullOrEmpty(text: string): boolean;
} 

String.ts (implementation)

String.prototype.isNullOrEmpty = function(text) => {
    if (text == null || text === "")
        return true;

    return false;
}

which can be used

if(!String.prototype.isNullOrEmpty(myStringToCheck)){
    // do things as of myStringToCheck is set to something
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.