Open jamiebuilds opened 3 years ago
I don't exactly know whether this is possible because files can overlap across projects. That would mean you'd create a type that could differ across different language services, even if they have the same type-checking flags configured.
Another issue here would be the difference between what people have in mind between
I was looking for a solution to the same problem. It currently seems impossible to infer the name of route params in file-based or directory-based routers. Mentioning meta.import.dir
, meta.import.file
and meta.import.path
here – which is what Bun and Deno use – so that this issue can be found easier. They also settled for emitting the current file's path *.ts
, not the emitted file's path *.js
.
To make this the same regardless of computer, we'd want the path relative to the nearest tsconfig.json
. And it should be the same type regardless of OS (/
vs \
). Given all that, for the current file /path/to/project/src/index.ts
we would want the path: ['src', 'index.ts']
But yes, I have to admit that this is a tall order, especially given that there is not even a standard JS function in any runtime that returns the corresponding runtime value.
Suggestion
🔍 Search Terms
import
meta
url
__filename
__dirname
✅ Viability Checklist
My suggestion meets these guidelines:
⭐ Suggestion
Provide current filename (relative to current
--project
) as a typeThis should be separate from
__filename
(which is an absolute path that can change depending on the location of a typescript project) andimport.meta.url
which is a fully qualified URL and may also change at runtime.📃 Motivating Example
An increasing number of web frameworks are using file system paths as a router with
$parameters.tsx
and such: Next, Gatsby, Remix, Nuxt, etcWith the addition of
`template literal ${types}`
people are now parsing out parameters from runtime routers.If the current filename was accessible in typescript, you could strictly type path segments without repeating the path name