我有这个函数:
function getProduct(id: string){
//return some product
}
id是实际上的GUID。Typescript没有guid类型。是否有可能手动创建类型GUID
?
function getProduct(id: GUID){
//return some product
}
所以,如果将'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
替换为某个'notGuidbutJustString'
,那么我将看到TypeScript编译错误。
更新:正如David Sherret所说:无法确保基于正则表达式或其他函数的字符串值在编译时进行检查,但可以在运行时的一个地方执行所有检查。
type Guid = string;
- Fred