0

I'm creating a framework where I'm giving developers the ability to create objects with custom types using defined metadata structures in XML:

<class name="Class">
    <attr name="id" type="Yuid" mandatory="true"/>
    <attr name="name"type="YString"/>
</class>

then in javascript/typescript you can do something like:

 const instance = new Class({
    name: "bob"
 });

The idea is to add validators to the class attribute generators to assert the XML schema is valid.

For simple primitives I was using the primitive constructors like str = new String(str) until we started running into some weird issues when displaying this data. To illustrate:

const thang = new String("thang");
const theng = String("theng");
const thing = "thing";
const thong = String({ rick: "sanchez" });
const thung = new String({ rick: "sanchez" });

console.log(thang, typeof thang); // [String: 'thang'] 'object
console.log(theng, typeof theng); // theng string
console.log(thing, typeof thing); // thing string
console.log(thong, typeof thong); // [object Object] string
console.log(thung, typeof thung); // [String: '[object Object]'] 'object'

console.log({}.toString(), typeof {}.toString()); // [object Object] string
console.log("abc".toString(), typeof "abc".toString()); // abc string
console.log([1, 2, 3].toString(), typeof [1, 2, 3].toString()); // 1,2,3 string
console.log((5).toString(), typeof (5).toString()); // 5 string

console.log(`thang is ${thang}`); // thang is thang
console.log(`theng is ${thang}`); // theng is theng
console.log(`thing is ${thang}`); // thing is thing
console.log(`thong is ${thong}`); // thong is [object Object]
console.log(`thung is ${thung}`); // thung is [object Object]

TypeScript hints are the same, I would be getting strings in all cases, but reality is a bit different so how can I be entirely sure than I can transform any value into string?

11
  • new String returns an object. To convert any value to a string, call String without new. Commented Mar 14, 2019 at 20:57
  • 1
    You should never use new String Commented Mar 14, 2019 at 20:58
  • @FelixKling check what happened when i did thong = String({}) Commented Mar 14, 2019 at 20:59
  • Yes, that's the expected value when converting an object to a string. Do you expect/want something else? It's not quite clear to me what your question is about tbh. Commented Mar 14, 2019 at 20:59
  • I expect to log/display a string and see the value of the string, not [object Object] Commented Mar 14, 2019 at 21:00

2 Answers 2

1

Instead of using:

const thang = new String("thang");

use:

const thang = "thang";
Sign up to request clarification or add additional context in comments.

Comments

0

In addition to the typeof check, you could check for the object's constructor's name

> var x = new String('hello')
> x
[String: 'hello']
> typeof x
'object'
> x.constructor.name
'String'

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.