0

In ts, I want the parameter of a function to be either a or b, for example:

let func = (str) => {}
// str can only be equal to one of'top' and'end'

I have been unable to find a solution.

  • 2
    Does this answer your question? [How to require a specific string in TypeScript interface](https://stackoverflow.com/questions/26855423/how-to-require-a-specific-string-in-typescript-interface) – Phil Sep 24 '20 at 05:09
  • function HelloWorld(str:string|number){ console.log("HELLO ", str) } – Ravinder Kumar Sep 24 '20 at 05:14

2 Answers2

1
let func = (str: 'top' | 'end') => {};

You are looking for string literal types in combination with union types.

Chris Yungmann
  • 1,079
  • 6
  • 14
0
 type Flow = {
    top: string,
    end: string
 }
let func = (str: keyof Flow) => {}