error TS2339: Property 'split' does not exist on type 'string | string[]'. Property 'split' does not exist on type 'string[]'

前端 未结 2 729
萌比男神i
萌比男神i 2020-12-16 13:57

When I build my application I get. error TS2339: Property \'split\' does not exist on type \'string | string[]\'. Property \'split\' does not exist on type \'string[

相关标签:
2条回答
  • 2020-12-16 14:34

    Brian Ball's solution is working, another approach for handling this situation is square brackets [ ].

    For instance

    req.headers.authorization[split](//define your conditions here)
    

    For exmaple:

    req.headers.authorization[split](",")
    
    0 讨论(0)
  • 2020-12-16 14:52

    The type you are trying to invoke split on is string|string[], which means that value may be either a string or a string[], in order for TypeScript to be happy, BOTH types must have a split method. If you are confident that it will always be a string, either update the definition (if possible) or cast to a string before calling split:

    (<string>req.headers.authorization).split
    

    or

    (req.headers.authorization as string).split
    
    0 讨论(0)
提交回复
热议问题