Upgrade typescript version#954
Conversation
|
Yeah, I don't think the loop is really worth it. Personally I’d probably go with something like function addCache(servicesHost: typescript.ModuleResolutionHost): {
moduleResolutionHost: typescript.ModuleResolutionHost,
clearCache: () => void,
} {
const clearCacheFunctions: Action[] = [];
return {
moduleResolutionHost: {
...servicesHost,
fileExists: createCache(servicesHost.fileExists),
directoryExists: servicesHost.directoryExists && createCache(servicesHost.directoryExists),
realpath: servicesHost.realpath && createCache(servicesHost.realpath),
},
clearCache: () => clearCacheFunctions.forEach(clear => clear()),
};
function createCache<TOut>(originalFunction: (arg: string) => TOut) {
const cache = new Map<string, TOut>();
clearCacheFunctions.push(() => cache.clear());
return function getCached(arg: string) {
let res = cache.get(arg);
if (res !== undefined) {
return res;
}
res = originalFunction(arg);
cache.set(arg, res);
return res;
};
}
}and then at the usage site let clearCache: null | (() => void) = null;
let moduleResolutionHost: ModuleResolutionHost = {
fileExists,
readFile: readFileWithFallback,
realpath: compiler.sys.realpath,
directoryExists: compiler.sys.directoryExists,
getCurrentDirectory: compiler.sys.getCurrentDirectory,
getDirectories: compiler.sys.getDirectories
};
if (enableFileCaching) {
const cached = addCache(moduleResolutionHost);
clearCache = cached.clearCache;
moduleResolutionHost = cached.moduleResolutionHost;
}I guess that’s slightly more wordy than what’s proposed here, but doesn’t contain any unsafe type assertions. |
|
Thanks for the suggestion @andrewbranch! @fa93hws do you fancy tweaking your PR with the suggested implementation? |
|
I feel like I should add that there are of course lots of different solutions that largely come down to personal preference of coding style. Avoiding unnecessary type assertions is a worthwhile goal, but I’m sure mine is not the most elegant possible solution. |
|
No problem! |
johnnyreilly
left a comment
There was a problem hiding this comment.
Looks good! Do you want to update the CHANGELOG.md? Remember to thank yourself 😁
| "tslint-config-prettier": "^1.15.0", | ||
| "typescript": "^3.1.1", | ||
| "typescript": "^3.5.1", | ||
| "webpack": "^4.5.0", |
There was a problem hiding this comment.
Shall we go for TypeScript 3.5.2 since it's out?
There was a problem hiding this comment.
No problem!
The version goes so quick
|
Thanks! |
Latest version typescript has more strict type check.