TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

No compilation error when concatenating function with string.

Open uglycoyote opened this issue 11 years ago • 6 comments

If you accidentally concatenate a ()=>string with a string, typescript does not give an error or warning, but I believe it should.

This is easily reproducible in the "Try Typescript" hello world greeter example.

originally I had some code that was akin to this:

meet() : string
{
    return this.greeting + ", sincerely George"
}

where I was concatenating a string property to another string.

Then, during a refactoring, I encapsulated the "greeting" part into a member function, greet(). I had intended to do something like this

meet() : string
{
    return this.greet() + ", sincerely George"
}

which would print "Hello World, sincerely George" when you click the button.

But I accidentally forgot the parentheses on greet:

meet() : string
{
    return this.greet + ", sincerely George"
}

which causes the alert button to print out the source code of the greet function, followed by "Sincerely George"

function () { return "Hello, " + this.greeting; }, sincerely George

One would think that TypeScript would warn you that you are concatenating a function's source code with a string to make another string.

It does warn you if you simply try to return the function as a string

meet() : string
{
    return this.greet
}

saying "Type '() => string' is not assignable to type 'string'."

But it seems that as soon as you try to concatenate a ()=>string with another string it merrily goes on its way converting functions to strings, which seems rather dangerous to me.

Is there a good reason why implicitly casting a function to a string containing its source code should be allowed?

uglycoyote avatar Feb 20 '15 07:02 uglycoyote

One would think that TypeScript would warn you that you are concatenating a function's source code with a string to make another string.

One would think so, but unfortunately it would warn users in the face of completely valid JavaScript.

function foo() {
    return 10;
}

// Prints out the text of the function.
// This is probably not what you wanted.
console.log("" + foo);

foo.toString = function() {
    return "Anything can happen in this language!";
}

// Prints 'Anything can happen in this language!'.
// Potentially desirable since 'toString' was overridden.
console.log("" + foo); 

In short, using "" + is just a common idiom for invoking toString and I don't think we'd be able to appropriately dictate which uses are valid.

Hope that answers your question!

DanielRosenwasser avatar Feb 20 '15 07:02 DanielRosenwasser

I would think concatenation of non-primitive types should be allowed only if they have a declared toString(): string member (as opposed to the implicit member inherited from Object). There should almost always be one in objects that someone would actually want to stringify implicitly.

jeffreymorlan avatar Feb 20 '15 21:02 jeffreymorlan

I agree with Jeffrey here. Certain, Daniel, you don't want to prevent people from legitimate use of the toString conversion.

But if toString is not overridden, it's hard to imagine a case where somewant would want to purposely implicitly concatenate their function source code with another string. If they really want to do that, the TypeScript compiler should force an explicit call to toString.

Likewise, if I really wanted the meet() function to return the source code of the greet() function, I could write

meet() : string
{
    return this.greet.toString()
}

and this tells TypeScript that "yes, I really do want to turn my function into a string". But as I mentioned before, without the toString, TypeScript helpfully tells you that you have a type mismatch. Which after all is the whole point of TypeScript.

It does not make sense to me that there is a double standard where you support dangerous implicit casts when concatenating strings, but not upon returning values.

uglycoyote avatar Feb 20 '15 21:02 uglycoyote

There's a huge amount of code out there that does stuff like this:

console.log("Foo is " + x);

where x might be a function, undefined or null, in which case explicitly calling toString would be a bad idea. We made the decision that adding anything to a string is so often done that it is not ever an error.

If we absolutely had to add an error case to +, this would indeed probably be the first choice. but it doesn't meet the bar for introducing a breaking change in the language.

RyanCavanaugh avatar Feb 20 '15 21:02 RyanCavanaugh

@RyanCavanaugh if this can be under a compiler flag, that would work for everyone. My colleague did run in the same scenario as described above, so it is a valid case. I get your point, this is how js is working, so with a flag people who are interested in extra security could turn it on. Cases like

console.log("Foo is " + x);

can be refactored to

console.log("Foo is ", x);
//or
console.log("Foo is " + String(x));
//or
console.log("Foo is " + (x as string));

webuniverseio avatar Jul 27 '16 02:07 webuniverseio

TSLint will soon (already?) have type-based rules so you can flag things like this at your discretion.

(x as string) is not a good workaround; this assertion essentially never succeeds in a useful way.

RyanCavanaugh avatar Jul 27 '16 05:07 RyanCavanaugh