0

I'm trying to join two Array like this:

const param = null
const optionsHeaders: Array<['string', 'string']> = param || []
const sesHeaders = [
    ['X-SES-CONFIGURATION-SET', 'config-set']
]
const headers: Array<['string', 'string']> = [...optionsHeaders, ...sesHeaders]
console.log(headers)

The result is as expected:

[
  [
    "X-SES-CONFIGURATION-SET",
    "config-set"
  ]
]

But the TypeScript compiler is complaining:

Type 'string[][]' is not assignable to type '["string", "string"][]'.
  Type 'string[]' is missing the following properties from type '["string", "string"]': 0, 1

const headers: Array<['string', 'string']> = [...optionsHeaders, ...sesHeaders]

What am I missing here?

1
  • 1
    : string = all possible strings, : 'string' = only the string 'string'. Yes, values can be types! Commented Jul 11, 2019 at 17:14

1 Answer 1

3

You need the type on sesHeaders as, by default an array literal [ 'foo', 'bar'] will have an inferred type of string[] and not [string, string].

Also, your 'string' don't need the single quotes because, as @nino-filio points out, constant values can be types too so by saying 'string' you're saying "only a string of value 'string'".

type Headers = [ string, string ][];

const param = null
const optionsHeaders: Headers = param || []
const sesHeaders: Headers = [
    ['X-SES-CONFIGURATION-SET', 'config-set']
]
const headers: Headers = [...optionsHeaders, ...sesHeaders]
console.log(headers);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.