Assemblyscript: Converting a number to a string

Created on 29 May 2019  路  11Comments  路  Source: AssemblyScript/assemblyscript

I read https://github.com/AssemblyScript/assemblyscript/issues/369, but I'm not sure how to do it.

I have

    assert(s.width === 60, 's.width is not 60, got' + s.width)

and the compiler says

ERROR AS200: Conversion from type 'f64' to '~lib/string/String' requires an explicit cast.

        assert(s.width === 60, 's.width is not 60, got' + s.width)

Is it possible?

question

All 11 comments

Ok, found a workaround:

    const a = new Array<f64>(1)
    a.push(s.width)
    assert(s.width === 60, 's.width is not 60, got ' + a.toString())

Currently you should explicitly call toString() method

@MaxGraey Ah, that indeed works, but I got confused by my editor showing an error:

Screen Shot 2019-05-29 at 8 18 44 AM

So I also was trying new Number(s.width), but that constructor doesn't take an argument. Is there some other way to do it without TS having an error?

You could fixed this by adding this to your file:

declare global {
  interface Number {
    toString(): string;
  }
}

@dcodeIO It seems we need wrap our definitions to global namespace

I tried to put in my code, and got:

ERROR TS1109: Expression expected.

        interface Number {
  ~~~~~~~~~
 in src/as/glas/index.ts(8,1)

As a workaround, what worked was placing

interface Number {
    toString(): string
}

in a sibling .d.ts file.

Yes,it better place in separate d.ts file

I'm guessing declare in a .ts file isn't supported yet by the compiler?

@MaxGraey Hello! Should the toString be added to the standard library types for numbers so users don't have to do it?

I haven't see any problem with master: https://webassembly.studio/?f=7eteo29co4o

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyegupov picture kyegupov  路  3Comments

pannous picture pannous  路  4Comments

kungfooman picture kungfooman  路  5Comments

andy-hanson picture andy-hanson  路  4Comments

MaxGraey picture MaxGraey  路  4Comments