Socket
Socket
Sign inDemoInstall

typesafe-path

Package Overview
Dependencies
0
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    typesafe-path

This library is aim to explicitly annotate path format of the code, and throwing error when passing incorrect format paths.


Version published
Weekly downloads
81K
decreased by-24.18%
Maintainers
1
Install size
4.56 kB
Created
Weekly downloads
 

Readme

Source

TypeSafe Path

This library is aim to explicitly annotate path format of the code, and throwing error when passing incorrect format paths.

The advantage is that we can avoid using upath to convert all paths to unix path, and ensure current code logic always correctly know the path format that should be processed.

Please note that you should only use it in your modules that are coupled to the filesystem.

Usage

import * as path from 'typesafe-path';

/**
 * win32: __dirname is 'c:\\foo\\bar.js'
 * posix: __dirname is '/foo/bar.js'
 */
const osPath = __dirname as path.OsPath;
const win32Path = '..\\aaa\\bbb' as path.Win32Path;

/**
 * win32: 'c:\\foo\\aaa\\bbb' <-- good result
 * posix: '/foo/bar.js/..\\aaa\\bbb' <-- bad result
 */
path.resolve(osPath, win32Path);
//                   ^ Argument of type 'Win32Path' is not assignable to parameter of type 'OsPath | PosixPath'.

// Fixs
const posixPath = win32Path.replace(/\\/g, '/') as path.PosixPath;
path.resolve(osPath, posixPath); // no type error

FAQs

Last updated on 11 Oct 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc