Skip to content

Latest commit

 

History

History

api

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

API Docs

Welcome to @safelytyped/filepath!

Introduction

What Is A Safe Type?

A safe type is a type that can only ever hold legal values.

For example, Filepath is a safe type because it can only ever hold a valid filesystem path (it just might not be valid on your computer!).

Why Use Safe Types?

If your function accepts a safe type, your function doesn't need to do its own defensive programming for robustness.

Safe types do the defensive programming for you.

On top of that, we build our safe types using written coding standards.

Why A Filepath Type?

You might be wondering why you should use Filepath, instead of a plain ol' string. There's a few reasons why:

  • Compile-time checking:

    If you just use strings everywhere, the compiler can't tell a file path from a username. It certainly can't tell you when you accidentally use a username instead of a file path.

    If you use types everywhere, then the compiler can spot when you've passed the wrong kind of string into a function.

  • A means to deliver functionality into business logic:

    If you pass typed values into your business logic, it can call methods on those types without having to know whether it's working with a Filepath or a URL.

    Value objects can implement protocols (interfaces that describe behaviours). And business logic can consume and use objects based solely on their protocols, without caring whether they're a Filepath or a URL.

  • Runtime extensibility:

    Along with protocols, value objects can be augmented at runtime with extensions. You don't have to submit a pull request to our repo and wait for us to merge it; you can create your own extension and use it locally straight away.

Our Goals

The purpose of this library is to give us the smallest possible, viable Filepath safe type that adds value to our code.

Our Design Criteria

There's a couple of things that we wanted out of Filepath.

  • Filepath's API should be very familiar to anyone who already knows NodeJS's path module.
  • Filepath should be useful for tracking a data location: a path that's built from a mix of a base path and a location relative to that base path.