Wildcard Matching Implementation in TypeScript
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Wildcard Matching runs in O(n*m) time and O(n*m) space, in TypeScript as in every other language.
- ✓At runtime TypeScript is JavaScript, so the matcher is the same RegExp translation or hand-written DP.
- ✓Reviewers want the pattern narrowed rather than left as a bare string: a template literal type like `${string}*${string}`, or a branded Glob type built by a validating factory, so an unvalidated user string cannot reach the matcher.
- ✓The TypeScript trap: Turn on noUncheckedIndexedAccess and every dp[i][j] and even every typed-array read becomes T | undefined, because it is an index-signature access.
- ✓The runtime cost is identical to JavaScript, because the types are erased and emit nothing.
What Wildcard Matching actually is
Wildcard Matching in TypeScript: what you actually reach for
TypeScript implementation
export function isMatch(s: string, p: string): boolean {
const m = p.length;
let prev = new Uint8Array(m + 1);
let cur = new Uint8Array(m + 1);
prev[0] = 1;
for (let j = 1; j <= m; j++) {
const base = prev[j - 1] ?? 0;
prev[j] = base === 1 && p.charCodeAt(j - 1) === 42 ? 1 : 0;
}
for (let i = 1; i <= s.length; i++) {
const sc = s.charCodeAt(i - 1);
cur[0] = 0;
for (let j = 1; j <= m; j++) {
const pc = p.charCodeAt(j - 1);
const left = cur[j - 1] ?? 0;
const up = prev[j] ?? 0;
const diag = prev[j - 1] ?? 0;
cur[j] = pc === 42
? (left | up)
: pc === 63 || pc === sc ? diag : 0;
}
const t = prev; prev = cur; cur = t;
}
return prev[m] === 1;
}
export type Matches<P extends string, S extends string> =
P extends `*${infer R}`
? Matches<R, S> extends true ? true
: S extends `${infer _H}${infer ST}` ? Matches<P, ST> : false
: P extends `?${infer R}`
? S extends `${infer _H2}${infer ST}` ? Matches<R, ST> : false
: P extends `${infer C}${infer R}`
? S extends `${C}${infer ST}` ? Matches<R, ST> : false
: S extends "" ? true : false;
type Ok = Matches<"a*c", "abbbc">;Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe TypeScript-specific trap
Performance in TypeScript, beyond the O(n*m)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which TypeScript type or library should I use for Wildcard Matching?
At runtime TypeScript is JavaScript, so the matcher is the same RegExp translation or hand-written DP. What TypeScript adds is a type layer: template literal types with infer can express the matcher as a compile-time predicate, so Matches<'a*c', 'abbbc'> resolves to true before the program runs. For real glob work, check each package before assuming @types is unnecessary: minimatch is written in TypeScript and ships its own .d.ts, while picomatch does not, so it needs npm i -D @types/picomatch or the import lands on TS7016 implicit-any. Node's own APIs are typed by @types/node.
What is the most common TypeScript mistake when implementing Wildcard Matching?
Turn on noUncheckedIndexedAccess and every dp[i][j] and even every typed-array read becomes T | undefined, because it is an index-signature access. The natural fix people reach for is a trailing !, and that is where the bug enters: the assertion silences the one genuine case where the row really was allocated m long instead of m + 1, and the out-of-range read becomes undefined, which is falsy, so the matcher quietly returns false instead of throwing. The type-level matcher has its own wall: a non-tail-recursive conditional type over a long string trips 'Type instantiation is excessively deep and possibly infinite' (ts2589) and the editor goes silent rather than reporting a wrong answer.
How does Wildcard Matching actually perform in TypeScript?
The runtime cost is identical to JavaScript, because the types are erased and emit nothing. The cost you actually pay is at compile time: the recursive conditional-type version of this matcher instantiates one type per character pair, so a handful of long literals can push tsc memory and make hover and completion in the editor lag by seconds. Keep the type-level matcher for short literal keys and route real data through the runtime DP. Cap the type-level matcher's depth explicitly with a counter parameter that resolves to never at zero, so a pathological literal fails fast at twenty characters instead of after tsc has instantiated thousands of types. Time complexity is O(n*m) and space is O(n*m).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what GitHub Copilot offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.