How do I define a type or interface describing a deeply nested array in TypeScript?
For example, let's say I am writing a function for testing a path against any number of patterns.
function match(path: string, matcher: Matcher): boolean { /* ... */ }
The Matcher type may be any of the following:
stringRegExpMatcher[](note the self-reference)
In other words, the compiler should accept the following:
match('src/index.js', 'lib/**/*');
match('src/index.js', /\/node_modules\//);
match('src/index.js', ['src/**/*', /\.js$/]);
match('src/index.js', ['src/**/*', [/\.js$/, ['*.ts']]]);
But the following should produce a compiler error:
match('src/index.js', {'0': 'src/**/*'}); // Compiler Error!!!
match('src/index.js', ['src/**/*', true]); // Compiler Error!!!
match('src/index.js', ['src/**/*', [/\.js$/, [3.14]]]); // Compiler Error!!!
Is there a way to achieve this in TypeScript?