最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - typescript - string literal type to type string - Stack Overflow

programmeradmin6浏览0评论

I have a method with string literal type guard

type Status = 'new' | 'update' | 'close';

itemRequest(status: Status) { 
    itemService.subscribeToChanges(status) //third party library method
}

Difficulty I have is this third party method accepts string not my custom string type Status

subscribeToChanges(status: string) 

How can I make my string literals are actually types of string and when I pass to third party method. It understand it's actually a string?

I have a method with string literal type guard

type Status = 'new' | 'update' | 'close';

itemRequest(status: Status) { 
    itemService.subscribeToChanges(status) //third party library method
}

Difficulty I have is this third party method accepts string not my custom string type Status

subscribeToChanges(status: string) 

How can I make my string literals are actually types of string and when I pass to third party method. It understand it's actually a string?

Share Improve this question asked Nov 16, 2016 at 19:18 MonteCristoMonteCristo 1,5502 gold badges21 silver badges44 bronze badges 1
  • 1 What exactly is the problem? You're getting pilation error when pass the status? What error? I'm unable to reproduce it. – Nitzan Tomer Commented Nov 16, 2016 at 20:19
Add a ment  | 

3 Answers 3

Reset to default 5

You are able to cast your value of the Status to a string like this:

type Status = 'new' | 'update' | 'close';

itemRequest(status: Status) { 
    itemService.subscribeToChanges(status as string)
}

Do not use String(status). This constructs a new string object from status and is a wasteful allocation. The cast will be erased at runtime and just corrects TypeScript's knowledge of the type of the status variable. Remember that any type checking TypeScript does is done at pile time and has no runtime cost nor presence.

This code will be piled to the following:

itemRequest(status) {
  itemService.subscribeToChanges(status)
}

I am however confused as to why this is happening. Your type alias are all of type string so casting from Status to string should be a upcast (i.e, permissible under any reasonable type checker). The other way around would be the main issue. Indeed, your original code works just fine on it's own.

Can you make Status an enum? Then you can use Status[status] which will be the string value you expect. i.e.

enum Status { new, update, close }

itemRequest(status: Status) { 
    itemService.subscribeToChanges(Status[status]) //third party library method
}

It should work. In JavaScript it is a normal string. Check out this sample or visit it in the playground.

// TypeScript 
type Status = 'new' | 'update' | 'close';

function itemRequest(status: Status) { 
    itemService.subscribeToChanges(status) //third party library method
}
let myStatus: Status = 'update';
itemRequest(myStatus);

// Compiled JavaScript
function itemRequest(status) {
    itemService.subscribeToChanges(status); //third party library method
}
var myStatus = 'update';
itemRequest(myStatus);
发布评论

评论列表(0)

  1. 暂无评论