I have been trying to locate a tslint rule in the tslint.yml
file that can identify and flag any usage of Indexable Types (such as { [key: string] : string }
) in favor of TypeScript Records (e.g. Record<string, string>
). However, I haven't had any success so far.
During my search, I came across a tslint rule called Ban-Types, which is designed to restrict the use of types matching specific patterns. Here's what I attempted:
tslint.yml
ban-types: [
true,
[ "{\s*\[\s*(\S+)\s*:\s*(.+)\s*\]\s*:\s*(.+)\s*}",
"Do not use indexable types (i.e. { [index: A] : B } ). Use Record<A, B> instead." ],
]
Unfortunately, this approach resulted in an error message:
unknown escape sequence at line 23, column 10:
[ "{\s*\[\s*(\S+)\s*:\s*(.+)\s*\]\s*: ...
^ in /path/to/tslint.yml
After revising the regex with double backslashes, the error disappeared but the rule still couldn't detect indexable types.
If you're interested, you can view the implementation of the rule here.