D
D
Darkhan Kamaliev2020-11-10 16:30:01
typescript
Darkhan Kamaliev, 2020-11-10 16:30:01

Does TypeScript make sense if on the same php backend?

The question is caused by a small misunderstanding, TS allows us to catch errors at the level of writing code .. but at the end we still get JS, without types and everything else. (here is still ok).
But we don't know exactly what type of data the backing will return (hello php with an attempt to return 3.0 as a number, not a string). Doesn't the advantage of TS fall here? If we cannot control the types on the back with the same precision?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Dmitry, 2020-11-10
@dimoff66

1) What you get as output in js - no one cares at all, no one in their right mind will read or edit the minified code. All typing is design-time order control.
2) PHP can only return one type of data - a string. If it is in json format, then the types in json are the same for all languages ​​and will be converted to js types during JSON.parse.
So do not worry, feel free to connect TS, typing is a good thing, especially since in comparison with languages ​​​​such as Java or C # in TS it is very sparing.

A
Alexey P, 2020-11-10
@ruddy22

Certainly. Backend is a completely different system.
The advantage of TS is to catch errors at the compilation stage (before running the code) in order to "hit" the hands of the JS developer if he writes garbage. If in general terms.


But we don't know exactly what type of data the backing will return (hello php with an attempt to return 3.0 as a number, not a string). Doesn't the advantage of TS fall here? If we cannot control the types on the back with the same precision?

To solve such problems, there are other tools - documentation. You can also pull Swagger ( https://swagger.io/ ).
If you want to check data types at runtime, then I advise you to look towards JSON Schema Validation ( https://json-schema.org/implementations.html ).
Swagger - relevant for documentation, because gives the API specification.
JSON Schema Validation - can be pulled both on the front and on the back.

D
Dmitry Kuznetsov, 2020-11-10
@dima9595

PHP (^7.0) has the same return type. You just need to specify the type of the returned data after the brackets.
This was already written about on Habré: https://habr.com/ru/post/248721/

I
Inviz Custos, 2020-11-10
@MvcBox

But we don't know exactly what type of data the backing will return (hello php with an attempt to return 3.0 as a number, not a string).

https://www.npmjs.com/package/typescript-is

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question