Skip to content

Instantly share code, notes, and snippets.

@hazzard993
Created April 11, 2019 06:57
Show Gist options
  • Save hazzard993/a950a469ee1b959355f61f1741cdf779 to your computer and use it in GitHub Desktop.
Save hazzard993/a950a469ee1b959355f61f1741cdf779 to your computer and use it in GitHub Desktop.

Filepath Registry

LÖVE 2D can interact with the file system.

love.filesystem.getLastModified("file.txt");

However, there likely is a FileRegistry stopping you.

If you don't care about why it is there, opt-out by adding "ts-love/no-file-registry" to your tsconfig's types.

{
    "compilerOptions": {
        "types": [
            "ts-love",
            "ts-love/no-file-registry"
        ]
    }
}

But if you want to keep it, you'll get...

  • File path autocompletion.
  • No missing path runtime errors.
  • File metadata with no overhead code.
  • Other registries contribute to this one.

How do you use it? Make a FileRegistry.d.ts file and put your available file paths within it.

interface FileRegistry {
    "files/file.txt": any;
}

Now when trying to access something from the file system, only the options in this registry are allowed.

TypeScript won't let any other file path be used and will inform you at compile time or even in your editor that a file path doesn't exist.

The overhead being you'll have to make sure this registry stays up to date. This could be done via a .sh script.

Hell you can even give the file metadata.

interface FileRegistry {
    "files/file.json": { type: "json" };
}

If you've good with your typings you can disallow json files from being used in the wrong context.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment