I am currently working on developing a codemod that will eliminate all instances of the $ReadOnly<T>
generic from a TypeScript codebase, while retaining only T
(where T
represents an object or union).
This is what I have managed to come up with so far:
module.exports = (fileInfo, api) => {
const j = api.jscodeshift
const source = j(fileInfo.source)
source
.find(j.TSTypeAliasDeclaration)
.find(j.TSTypeReference)
.filter(x => {
return x.value.typeName.name === '$ReadOnly' && x.value.typeParameters.params[0].type === 'TSTypeLiteral'
})
.replaceWith(nodePath => {
const members = []
nodePath.value.typeParameters.params[0].members.forEach(x => {
members.push(j.tsPropertySignature(x.key, x.typeAnnotation))
})
return j.tsTypeLiteral(members)
})
return source
.toSource()
}
The intention is to convert something like this:
export type MyType = $ReadOnly<{
someProps: string,
}>
To this:
export type MyType = {
someProps: string,
}
However, the result includes a duplicate type
keyword as shown below:
export type type MyType = {
someProps: string,
}
Any insights on what could be causing this issue?