0

ok so recently i changed my file from index.js to index.ts, here is a example of my problem

let response = "none"
let condition = true

if(condition){
 response = {id: 123 , data: []}
}

console.log(response)

but its giving me a syntax error saying {id: 123 , data: []} is not assignable to type string this is kinda frustraiting because i have over 5k lines of code and i dont feel like changing all the variables

Onii-Chan
  • 57
  • 1
  • 6
  • 3
    What was the reason of switching to TypeScript then? And what about the code working with those variables, does it expect strings or objects? – raina77ow Jun 22 '21 at 10:47
  • to do fancy function suggestion things – Onii-Chan Jun 22 '21 at 10:48
  • also it lets me know errors at dev – Onii-Chan Jun 22 '21 at 10:48
  • 4
    Well, it just did exactly that. TS tries its best to warn you both about existing issues and about the ones that might be introduced by some innocent looking changes. – raina77ow Jun 22 '21 at 10:49
  • idk how to phrase this but u dont have to worry about that – Onii-Chan Jun 22 '21 at 10:49
  • 1
    Yes you do. The problem is that sort of thing is *usually* a mistake, and the *whole point* of using a static type system is to catch it. If you have a lot of code like that, you can't use Typescript. At least not for the code where you do that, it would take a rewrite to get it past the compiler. – Jared Smith Jun 22 '21 at 10:50
  • when i have switched project to typescript i let there be js and ts together. and in time try to switch all the files to ts. my suggestion is do the same, start rewriting few files and go on. and this kind of issues you have FIX!. or let there be js. – Juraj Kocan Jun 22 '21 at 11:49

2 Answers2

1

In order for typescript to be useful, you need to actually type your properties and your methods.

In your example, the response variable can either be:

  • a 'none' string
  • an object with an ID and an array of data

Let's tell that to typescript:

type ResponseObject = { id: number, data: any[] }

let response: "none" | ResponseObject = "none"
let condition = true

if(condition){
 // No errors here, because typescript now knows that this data type is ok
 response = {id: 123 , data: []}
}

console.log(response)
gbalduzzi
  • 9,356
  • 28
  • 58
-2

use the any type

let response : any = "none"

but you will lose the suggestion benefit

otherwise you can also do

let response : (string | {id: Number , data: Array<any>} }= "none"

and you get some typescript suggestions for those types.

Godev
  • 316
  • 1
  • 4
  • 12
  • 2
    Suggesting `any` for type problems in *Type*Script is poor advice. – Clashsoft Jun 22 '21 at 11:08
  • 1
    At the *very least* suggest `unknown` instead of `any`. The `unknown` type [is a bit safer](https://stackoverflow.com/questions/51439843/unknown-vs-any). With that said, offering an "out" of the type checking is usually not a good idea. Especially not as the first thing to do. It mostly defeats the purpose of all of TS. ...and then we get more questions about "why didn't TS save me from doing X" in a code littered with `any` annotations. – VLAZ Jun 22 '21 at 11:12