What would be an idiomatic directory structure for a TypeScript project?
I would like the following features in such a structure:
Like any other class, function or object, you can explicitly export and import types from . ts files. Maybe these are TS files that contain nothing but types. These can be kept in the root folder or locally in the specific directory.
json / -p , existing TypeScript project properties will not be respected - all settings should be managed using your tsconfig file.
I would recommend generating a single-file output. Be it browser or for Node, its just a better idea. Bear in mind that most IDEs can hide .gitignore
d files, so an uncluttered file-pane shouldn't be a problem to attain, even if you let the .js
files sit right next to their .ts
files.
You can technically use --outDir
to output the way you want by setting up your tsconfig.json
appropriately.
This is fairly trivial! Just maintain a /tests
. Imports work simply by directory traversal like so import {MyClass} from "../src/modules/my-class"
(where the ../
is to get out of /tests
).
This is more challenging in the browser than on Node — the latter has require
s working out of the box for TypeScript.
Strongly recommend you go with something like webpack
, but if you insist on living life on the dangerous side, here is a browser-friendly require that I use to rapidly iterate on TypeScript code without having a build process setup.
require()
for the browser
Since absolute paths are necessary for working web imports, here is how you can use my require()
hack with TypeScript (typically for a fast debugging session that doesn't require rebuilding).
/entities/user.ts
import {Username} from "../entities/username"; import {Password} from "../entities/password"; export class User { username: Username; password: Password; }
Where Username
and Password
are export
ed classes in /entities/username.ts
and /entities/password.ts
respectively.
While the ../entities/
might seem extraneous, notice that it is essential for the browser to have appropriate absolute paths to our Username
and Password
entities. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With