Categories
TypeScript Answers

How to fix the “Property ‘matchAll’ does not exist on type ‘string'” error with TypeScript?

Sometimes, we want to fix the "Property ‘matchAll’ does not exist on type ‘string’" error with TypeScript.

In this article, we’ll look at how to fix the "Property ‘matchAll’ does not exist on type ‘string’" error with TypeScript.

How to fix the "Property ‘matchAll’ does not exist on type ‘string’" error with TypeScript?

To fix the "Property ‘matchAll’ does not exist on type ‘string’" error with TypeScript, we can add the "es2020.string" entry into compilerOptions.lib in tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    "lib": ["es2020.string"]
  }
  //...
}

to add the "es2020.string" entry into compilerOptions.lib in tsconfig.json.

Then the TypeScript compiler knows that the string matchAll method is available for use in our TypeScript project.

Conclusion

To fix the "Property ‘matchAll’ does not exist on type ‘string’" error with TypeScript, we can add the "es2020.string" entry into compilerOptions.lib in tsconfig.json.

Categories
TypeScript Answers

How to use computed properties in Vue.js with TypeScript?

To use computed properties in Vue.js with TypeScript, we can add getters.

For instance, we write

<template>
  <div>
    <input type="text" name="Test Value" id="" v-model="text" />

    <label>{{ label }}</label>
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator";

@Component({})
export default class About extends Vue {
  private text = "test";

  get label() {
    return this.text;
  }
}
</script>

to add the label getter that returns the value of the this.text reactive property.

Conclusion

To use computed properties in Vue.js with TypeScript, we can add getters.

Categories
TypeScript Answers

How to sort an array of objects in TypeScript?

Sometimes, we want to sort an array of objects in TypeScript.

In this article, we’ll look at how to sort an array of objects in TypeScript.

How to sort an array of objects in TypeScript?

To sort an array of objects in TypeScript, we can use the array sort method like we do with JavaScript.

For instance, we write

const sortedArray: Test[] = unsortedArray.sort((obj1, obj2) => {
  if (obj1.value > obj2.value) {
    return 1;
  } else if (obj1.value < obj2.value) {
    return -1;
  }
  return 0;
});

to call unsortedArray.sort with a callback to sort the entries in the array and return it.

We compare the value of the value property in each object.

And we return 1, -1, or 0 depending if we want to put obj2 before obj1, obj1 before obj2 or keep the items in the same order respectively.

unsortedArray is unchanged after calling sort, it returns a new array with the sorted entries.

Conclusion

To sort an array of objects in TypeScript, we can use the array sort method like we do with JavaScript.

Categories
TypeScript Answers

How to fix the “property ‘assign’ does not exist on type ‘ObjectConstructor'” error with TypeScript?

Sometimes, we want to fix the "property ‘assign’ does not exist on type ‘ObjectConstructor’" error with TypeScript.

In this article, we’ll look at how to fix the "property ‘assign’ does not exist on type ‘ObjectConstructor’" error with TypeScript.

How to fix the "property ‘assign’ does not exist on type ‘ObjectConstructor’" error with TypeScript?

To fix the "property ‘assign’ does not exist on type ‘ObjectConstructor’" error with TypeScript, we should add 'es6' to compilerOptions.lib in tsconfig.json.

For instance, we write

{
  //...
  "compilerOptions": {
    "lib": ["es5", "es6", "dom", "es2015.collection"]
  }
  //...
}

to add "es6" to compilerOptions.lib in tsconfig.json so that the TypeScript compiler knows that the Object.assign method that comes with ES6 is available for use in our project.

Conclusion

To fix the "property ‘assign’ does not exist on type ‘ObjectConstructor’" error with TypeScript, we should add 'es6' to compilerOptions.lib in tsconfig.json.

Categories
TypeScript Answers

How to export a constant in TypeScript?

Sometimes, we want to export a constant in TypeScript.

In this article, we’ll look at how to export a constant in TypeScript.

How to export a constant in TypeScript?

To export a constant in TypeScript, we can use the export keyword.

For instance, we write

./docs/users/admin.ts

export const adminUser = {
  //...
};

to export the adminUser constant.

Then we can import adminUser in another module by writing

import * as users from "./docs/users/admin";

const { adminUser } = users;

to import the whole module and name the imported module users with

import * as users from "./docs/users/admin";

Then we can reference adminUser with

const { adminUser } = users;

Conclusion

To export a constant in TypeScript, we can use the export keyword.