D
D
Dmitry Ivchenkov2019-07-22 22:06:40
JavaScript
Dmitry Ivchenkov, 2019-07-22 22:06:40

How to fix missing definitions of some standard js methods in typescript?

I am writing a custom pipe. At the input and output - a string (9-digit number is expected).
It is necessary to bring 123456789 to the form (12) 345-67-89.
I tried:

value.splice ( 0, 0, "(" );
value.splice ( 2, 0, ") " );

But the tslint console throws an error. No method defined for string. Moreover, in the browser this method (on pure js) works properly.
Maybe I'm approaching the solution incorrectly or do I need to write the function prototype in *.d.ts?
In general, I don't know how to make this pipe work..

Answer the question

In order to leave comments, you need to log in

3 answer(s)
0
0xD34F, 2019-07-22
@Dzmitryj_Black

in the browser this method (on pure js) works properly

Somehow I don't believe it.
Yes, cut the string with the usual slice:
`(${value.slice(0, 2)}) ${value.slice(2, 5)}-${value.slice(5, 7)}-${value.slice(7)}`

A
Anton Spirin, 2019-07-22
@rockon404

"compilerOptions": {
    "lib": ["es6", "dom"],
}

P
profesor08, 2019-07-22
@profesor08

There is no such method for strings. There is such a method for arrays. String !== Array. Break the string into an array of characters, twist them as you like, then put them back together.

let value = "123456789";
let numbers = value.split("");
numbers.splice ( 0, 0, "(" );
numbers.splice ( 3, 0, ")" );
numbers.join(""); // (12)3456789

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question