Reputation: 2376
I am trying to create a method which parameter has fixed string value types but at the same time i want user to also provide value other than fixed string values.
function create(options: "A" | "B" ){
}
create("C");
Here when user is passing "C" value, typescript is throwing error. This can be fixed by casting "C" to any, but i don't want user to do anything but my method should take care of it.
Consider the options contains 100 of string values, so it is helping users to get those value in intillisense which will help users to don't remember it and use it.
Can i do anything from methods ?
Upvotes: 0
Views: 852
Reputation: 33101
I think it is better to overload your function:
function create(options: "A"): void
function create(options: "B"): void
function create(options: string): void
function create(options: string) { }
create()
IDE will be able to pick up allowed arguments along with string
type:
Upvotes: 1