I am researching code conventions in TypeScript and C# and we have figured a rule to use string.Empty
instead of ""
in C#.
C# example:
doAction(""); doAction(string.Empty); // we chose to use this as a convention.
TypeScript:
// only way to do it that I know of. doAction("");
Now is my question is there a way to keep this rule consistent in TypeScript as well or is this language specific?
Do any of you have pointers how to define an empty string in TypeScript?
Yes. All false , 0 , empty strings '' and "" , NaN , undefined , and null are always evaluated as false ; everything else is true .
TypeScript Null is much like void, i.e. not useful on its own. By default, null is a subtype of all other subtypes which means a user can assign null to any of the data types like string, number, etc.
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.
If you really want to do that, you could write code to do this:
interface StringConstructor { Empty: string; } String.Empty = ""; function test(x: string) { } test(String.Empty);
As you can see, there will be no difference in passing String.Empty
or just ""
.
There is a type String
which has a definition found in lib.d.ts
(there are also other places this library is defined). It provides type member definitions on String
that are commonly used like fromCharCode
. You could extend this type with empty
in a new referenced typescript file.
StringExtensions.ts
declare const String: StringExtensions; interface StringExtensions extends StringConstructor { empty: ''; } String.empty = '';
And then to call it
otherFile.ts
doAction(String.Empty); // notice the capital S for String
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With