POST POST

SEP
28
2020

The trimStart rabbit hole

I was bragging to David about a particularly impressive piece of TypeScript code I wrote last week

1
if (body.trim().startsWith('<')) { //100% infallible xml detection

He, rightly, pointed out that trimStart would probably be more efficient. Of course it would! However when I went to make that change there was only trim, trimLeft and trimRight in my TypeScript auto-complete drop down.

TrimStart and TrimEnd are missing

Odd. This was for sure a real function because it appears in the MDN docs.

A reasonable person would have used trimLeft and moved on but it was Monday and I was full of passion for programming. So I went down the rabbit hole.

Checking out the TypeScript directory in my node_modules I found that there were quite a few definition files in there. These were the definition files that described the JavaScript language itself rather than any libraries. Included in that bunch was one called lib.es2019.string.d.ts. This file contained changes which were made to the language in es2019.

1
2
3
4
5
6
7
8
9
10
11
12
13
interface String {
/** Removes the trailing white space and line terminator characters from a string. */
trimEnd(): string;

/** Removes the leading white space and line terminator characters from a string. */
trimStart(): string;

/** Removes the leading white space and line terminator characters from a string. */
trimLeft(): string;

/** Removes the trailing white space and line terminator characters from a string. */
trimRight(): string;
}

So I must be targeting the wrong thing! Sure enough in my tsconfig.js I was targeting es5 on this project. When we started this was using an older version of node on lambda that didn't have support for more recent versions of ES. I checked and the lambda was running node 12.18.3 and support for ES2020 landed in node 12.9 so I was good to move up to es2020 as a target.

Incidentally you can check the running node version in JavaScript by running

1
console.log('Versions: ' + JSON.stringify(process.versions));

After updating my tsconfig.js and restarting the language server all was right in the world.

The missing functions appear


Simon Timms

Email Email
Web Web
Twitter Twitter
GitHub GitHub
RSS

Looking for someone else?

You can find the rest of the Western Devs Crew here.

© 2015 Western Devs. All Rights Reserved. Design by Karen Chudobiak, Graphic Designer