0

this my code

file.js

const channel = {},
arr = [string,string,string];
for(let i = 0;i < arr.length;i++ ){
    channel[arr[i]] = "Amo" //equal string value
}

An array contains only string values. example ["a","b","c","n"]. How can I write this code in typescript ?

4
  • arr: String[] = [..] Commented Oct 23, 2019 at 13:01
  • How can you write what exactly in Typescript…? Commented Oct 23, 2019 at 13:01
  • How to write my code in typescript ? Commented Oct 23, 2019 at 13:04
  • @adiga What did I explain wrong? I wrote js code.I want to convert my code to typescript Commented Oct 23, 2019 at 13:16

1 Answer 1

2

The only difference between JS and TS is the concept of strongly typing your code. In this case, you want to migrate this code from JS to TS, you shouldn't need to do anything as the TS transpiler will be able to infer the types of the variables. If you do wants to enforce the type for the variables, you can do:

interface channelObj {
    [key: string]: string
} // This is called index signature and is useful for typing object key-value
const channel: channelObj = {},
arr: string[] = [string, ...];
for(let i = 0;i < arr.length;i++ ){
    channel[arr[i]] = "Amo" //equal string value
}
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.