Skip to content

Instantly share code, notes, and snippets.

@dovidweisz
Created November 24, 2020 18:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dovidweisz/4d76168339db89cf05e1090a84b7ee41 to your computer and use it in GitHub Desktop.
Save dovidweisz/4d76168339db89cf05e1090a84b7ee41 to your computer and use it in GitHub Desktop.
Patch react-use types for Typescript 4
const fs = require("fs");
const path = require("path");
/**
*
* Patching react-use for Typescript 4.1
*
* The latest version of typescript causes changs the name of a type used by react-use.
*
* https://github.com/streamich/react-use/issues/1646
*
* Patching it until the issue is resolved.
*/
const tsfilePath = path.resolve("node_modules", "react-use", "lib", "useGeolocation.d.ts");
const testExpression = /([^n])PositionError/;
fs.readFile(tsfilePath, (err, content) => {
if (err) {
throw err;
}
const contentString = `${content}`;
if (testExpression.test(contentString)) {
console.log(`Patching ${tsfilePath} to be compatible with TypeScript 4.`);
const newContent = contentString.replace(testExpression, "$1GeolocationPositionError");
fs.writeFile(tsfilePath, newContent, err => {
if (err) {
throw err;
}
console.log(`Sucessfully patched ${tsfilePath}`);
});
} else {
console.log(`${tsfilePath} already patched.`);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment