-1

How should I use Try/Catch in TypeScript? I don't want it to check for any errors in a given block because I already check for them (the error is expected and the state is controlled by the catch).

I have the code wrapped in Try/Catch (just example):

try {
    let data = localStorage.getItem('something');
    // Err. TS2345; I know, that's why you're in the try block
    data = JSON.parse(data);
    // Err. TS2531; I know, that's why you're in the try block
    data.exp = new Date(data.exp * 1000);
    return data;
} catch {
    return null;
}

TypeScript refuses to compile it. Is there any way I can tell TypeScript that I know about the error and I'm okay with it? I don't want to use //@ts-ignore on every line (assuming I can have a longer code) and I don't want to turn off strict mode. Is there any better solution?

Jax-p
  • 7,225
  • 4
  • 28
  • 58
  • 1
    `catch (_e) { return null; }`. You can't just write `catch { }` – eroironico Nov 30 '21 at 09:49
  • 1
    @Nick - Just `} catch {` is valid now, but I don't think that's what they're asking about. I think the issue is the things a bit hidden in the code comments. – T.J. Crowder Nov 30 '21 at 09:50
  • I want TypeScript to ignore errors in **try** block (because thats why it is in try block) and **empty catch is valid** but thank you. – Jax-p Nov 30 '21 at 09:51
  • These are typescript errors and not runtime errors. TS errors are not related to runtime and code executions error. Try/Catch are for runtime errors, so use `ts-ignore` to ignore them. – Naor Levi Nov 30 '21 at 09:54
  • @NaorLevi I know it is not runtime. – Jax-p Nov 30 '21 at 09:54
  • 1
    Write it in a more type-safe way, e.g. https://tsplay.dev/N9pVjw. – jonrsharpe Nov 30 '21 at 09:59
  • @jonrsharpe Out of curiosity, why doesn't the first param of `JSON.parse` accept `null` in addition to a string (or as an overload)? V8 is happy to accept it and return `null`. – jsejcksn Nov 30 '21 at 10:03
  • @jsejcksn I don't know, you'd have to ask the maintainers of the lib types where it's defined: https://github.com/microsoft/TypeScript/blob/0f3d0e047014ca71f0b8b457aa83cc8590bc5650/lib/lib.es5.d.ts#L1052-L1074 – jonrsharpe Nov 30 '21 at 10:07
  • @jonrsharpe Thank you. I got it but this is just an example. Sometimes you want to try/catch something and you are expecting an error. – Jax-p Nov 30 '21 at 10:07
  • @jsejcksn indeed it looks like people already have: https://github.com/microsoft/TypeScript/issues/43232 – jonrsharpe Nov 30 '21 at 10:08
  • @jonrsharpe I should have done a bit more research first (the spec specifies a string): https://stackoverflow.com/a/46464692/438273 – jsejcksn Nov 30 '21 at 10:10
  • @Jax-p but if what you're expecting is a type-related error the compiler can already tell you about, you probably shouldn't. – jonrsharpe Nov 30 '21 at 10:13

1 Answers1

3

TypeScript's purpose is to help you with coding errors caused by type issues. It's giving you TS2345 because you're passing string | null into JSON.parse (which expects just string), and it's giving you TS2531 and TS2339 because your code is using data as though it won't be null (but it may well be) and as though it has an exp property (which as far as TypeScript knows, it doesn't).

If you want to disable type checking for data, use the any type:

try {
    let data: any = localStorage.getItem('something');
//          ^^^^^−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−− here
    data = JSON.parse(data);
    data.exp = new Date(data.exp * 1000);
    return data;
} catch {
    return null;
}

From the linked documentation:

any

TypeScript also has a special type, any, that you can use whenever you don’t want a particular value to cause typechecking errors.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • I understand the errors and TypeScript, but I want to control the application by using errors and catching them. So the only option is to re-type it to **any**? I don't like that, but it's better than ts-ignore. Thank you. – Jax-p Nov 30 '21 at 09:57