Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Property 'replace' does not exist on type 'string'

Tags:

typescript

I get a strange error TS2339: Property 'X' does not exist on type 'Y'. How can I fix this?

I have added libraries to my 'tsconfig.jsonc' file:

"compilerOptions": {
    "target": "es3", // "es3" /* Specify ECMAScript target version: 'ES3' (default), 'ES5', 'ES2015', 'ES2016', 'ES2017','ES2018' or 'ESNEXT'. */,
    "watch": true,
    "module": "commonjs" /* Specify module code generation: 'none', 'commonjs', 'amd', 'system', 'umd', 'es2015', or 'ESNext'. */,
    "lib": [
      "esnext",
      "es2018",
      "es2017",
      "es2016",
      "es2015",
      "es7",
      "es6",
      "es5",
      "dom",
      "dom.iterable",
      "ScriptHost"
    ] /* Specify library files to be included in the compilation. */,
...
}

The code

const userName: string = groupAddress.replace('@gmail.com', '')

returns the error

Property 'replace' does not exist on type 'string'.

Similarly,

const addMembers = (email: string, studio: string, role): void => {
  const memberKey = email.trim()

returns

Property 'trim' does not exist on type 'string'.

  const groupKeys: string[] = [
    `report.${name}@gmail.com`,
    `support.${name}@gmail.com`
  ]
  groupKeys.forEach((groupKey: string) => {
    if (!GroupsApp.getGroupByEmail(groupKey).hasUser(memberKey)) {
      AdminDirectory.Members.insert({ email: memberKey, role }, groupKey)
    }
  })

returns

Property 'forEach' does not exist on type '{}'.

I expect

  • the type string has the method 'replace', and
  • the type string[] has the method 'forEach'.

But typescript says they do not.

like image 565
user174094 Avatar asked May 05 '19 07:05

user174094


People also ask

How do you fix property does not exist on type?

The "Property does not exist on type '{}'" error occurs when we try to access or set a property that is not contained in the object's type. To solve the error, type the object properties explicitly or use a type with variable key names. Copied!

Does not exist in type string?

The "Property does not exist on type String" error occurs when we try to access a property that does not exist on the string type. To solve the error, use an object instead of a string, or make sure you're accessing a valid built-in method on the string.

How do I use replaceAll in TypeScript?

To replace all occurrences of a string in TypeScript, use the replace() method, passing it a regular expression with the g (global search) flag. For example, str. replace(/old/g, 'new') returns a new string where all occurrences of old are replaced with new . Copied!

When was replaceAll added to JavaScript?

replaceAll() was introduced in Chrome 85 (August 2020) and Node. js 15. As of 2022, we do not recommend using replaceAll() due to limited support.


1 Answers

You are targeting es3, but have told TS that it has all the libraries for up to bleeding edge available to it. Without polyfills, this is a really bad idea.

TS will NOT functionally polyfill. It only adds syntactical polyfills. So you are setting a trap for yourself. Remove the lib entries, update your target to a modern target, or add in polyfills for everything there.

Also, as mentioned, I hope you tsconfig is tsconfig.json, and not tsconfig.jsonc. Otherwise that is your issue.

like image 144
Tim Avatar answered Sep 17 '22 16:09

Tim