TypeScript - 特定的字符串类型 [英] TypeScript - specific string types

查看:41
本文介绍了TypeScript - 特定的字符串类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种更好的方法来区分程序中不同类型的字符串 - 例如,绝对路径和相对路径.如果我搞砸了,我希望能够让函数采用或返回带有编译器错误的特定类型.

I'm looking for a better way to distinguish between different types of strings in my program- for example, absolute paths and relative paths. I want to be able to have functions take or return a certain type with a compiler error if I mess it up.

例如

function makeAbsolute(path: RelativePath): AbsolutePath {
}

其中 AbsolutePath 和 RelativePath 实际上只是字符串.我尝试了类型别名,但实际上并没有创建新类型.还有接口 -

where AbsolutePath and RelativePath and really just strings. I experimented with type aliases, but those don't actually create a new type. Also interfaces -

interface AbsolutePath extends String { }
interface RelativePath extends String { }

但是由于这些接口是兼容的,编译器不会阻止我将它们混在一起.我不知道如何在不向接口添加属性以使它们不兼容(并且将该属性实际添加到字符串或对其进行转换)或使用包装类的情况下执行此操作.还有其他想法吗?

but since those interfaces are compatible, the compiler doesn't prevent me from mixing them up. I don't see how I can do this without either adding a property to the interface to make them not compatible (and either actually adding that property to the string, or casting around it), or using a wrapper class. Any other ideas?

推荐答案

abstract class RelativePath extends String {
    public static createFromString(url: string): RelativePath {
        // validate if 'url' is indeed a relative path
        // for example, if it does not begin with '/'
        // ...
        return url as any;
    }

    private __relativePathFlag;
}

abstract class AbsolutePath extends String {
    public static createFromString(url: string): AbsolutePath {
        // validate if 'url' is indeed an absolute path
        // for example, if it begins with '/'
        // ...
        return url as any;
    }

    private __absolutePathFlag;
}

var path1 = RelativePath.createFromString("relative/path");
var path2 = AbsolutePath.createFromString("/absolute/path");

// Compile error: type 'AbsolutePath' is not assignable to type 'RelativePath'
path1 = path2;

console.log(typeof path1); // "string"
console.log(typeof path2); // "string"
console.log(path1.toUpperCase()); // "RELATIVE/PATH"

这在各个层面上都是错误的,你可以写一本关于它的书...... - 但它确实工作得很好,并且它确实完成了工作.

This is just so wrong on every levels you could write a book about it... -- but it does work nicely, and it does get the job done.

由于它们的创建是这样控制的,AbsolutePathRelativePath 实例是:

Since their creation is controlled as such, AbsolutePath and RelativePath instances are:

  • 被 TS 编译器认为彼此不兼容(因为私有属性)
  • 被TS编译器认为是(继承自)String,允许调用字符串函数
  • 运行时实际上是真正的字符串,为假定继承的字符串函数提供运行时支持

这类似于带有附加数据验证的伪造继承"(因为 TS 编译器被告知继承,但该继承在运行时不存在).由于没有添加公共成员或方法,这绝不会导致意外的运行时行为,因为在编译和运行时都存在相同的假定功能.

This is analogous to a "faked inheritance" (as the TS compiler is told about an inheritance, but that inheritance does not exist at runtime) with additional data validation. Since no public members or methods were added, this should never cause unexpected runtime behavior, as the same supposed functionality exists both during compilation and runtime.

这篇关于TypeScript - 特定的字符串类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆