0

I do a REST request and get back JSON. Assume recent versions of Angular/TS.

What is 'best practice' here:

  1. Do nothing with the received JSON in terms of Types and just treat it as 'any' and put its data in the destination object(s).
const localTypedObj: UserData;
localTypedObj.userId = dataFromRestReq.userId
  1. Give the incoming JSON a type/name and then use that:
type NewTypeName = typeof dataFromRestReq;
const newlyTypedData: NewTypeName = dataFromRestReq;

localTypeObj.userId = newlyTypedData.userId; // Here the editor knows that userId is a thing!
  1. Create a Type/Interface to then apply to the incoming data:
interface NewTypeName {
userId: string;
}
// elsewhere...
let newlyTypedData = <NewTypeName>dataFromRestReq;
  1. Other?

Am I restricting things too much here, if an extra item is added to the JSON by back-end folks, will front-end crash if the type is expecting a certain format?

2
  • 2
    This may be opinion-based, but I think it's good practice to add types at the earliest point possible. If the data received from the backend does not match the type, your frontend will not crash, as TypeScript does the type checking only at build time and not at runtime (see the docs). Commented Feb 17, 2023 at 11:50
  • 1
    I agree to the previous comment and would challenge anyone to tell me an advantage of using any instead of a correct typing. In the any case, your application might indeed crash because of typos or refactorings not picking up the instance because there is no type. Best practice is actually to have a shared app definition for client and server to avoid errors in communication.
    – Loop
    Commented Feb 17, 2023 at 13:39

1 Answer 1

1

It depends on what you do with the data.

If you only read it, so you can use a interface. It helps to avoid typos and makes all better readable. Sample:

export enum UserRole {
  admin = 10,
  guest = 5,
}

export interface UserLogin {
  userName: string;
  role: UserRole;
  token: string;
}

...

this.httpClient.get<UserLogin>(......).subscribe(data => {
  data.[no typos and a clean way] // Save to localStorage and so on
})

If you work with the data; so you wanna manipulate it as example, it is a good way to use a class:

export class UserLogin {
  public userName: string;
  public role: UserRole;
  public token: string;

  constructor() { }

  sayHello() {
    return "Hello " + userName + "!";
  }
}

...

this.httpClient.get<UserLogin>(......).subscribe(data => {
  const newUser = Object.assign(new UserLogin(), data)
  this.users.push(newUser); // Or do anything
  newUser.sayHello();
})

Many ways are ok. So nothing is wrong to use a class, a interface a enum or anything else. It depends only on what you wanna do with the data.

I hope it makes it more clear for you.

1
  • Thanks for this, you are say to eliminate option 1 (where JSON data is used and not immediately typed), and option 4 (as-in there is no 'other'!). Commented Feb 17, 2023 at 12:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.