0

enter image description hereis it a good idea to use interface instead of using any as a variable type. say i have 3 functions where i am defining variables which can be of alphanumeric format. Hence instead of defining it as of any can i do as an interface

  • yes its always a good practive to avoid any and create interface for types. interface helloworld { name: string; } https://stackoverflow.com/questions/37233735/typescript-interfaces-vs-types – Vibs May 27 '20 at 10:26
  • Yes, it's a good idea to use actual types. If you just use `any`, then why are you even employing **Type**Script and ditching the **types**? – VLAZ May 27 '20 at 10:26
  • say i have multiple variables in each function of type any, to remove type any, i will have to create multiple interfaces? –  May 27 '20 at 10:29

2 Answers2

1

Yes it is the right way. The goal of TS is to provide types instead of any values from javascript.

The goal is to have all variables typed, if you don't know its type - try to use generic or unknown instead of any. A type def is more extensible than an interface def. An interface works for objects and classes, a type works for everything including primitives, unions and infers, but a class can implement interfaces only, not types.

If you need to assert type you can use type guards:


const isNumber = (value: unknown): value is number {
  return typeof value === 'number';
}

const something: any = 123;
something += 1; // valid
something.callFake(); // valid

if (isNumber(something)) {
  something += 1; // valid
  something.callFake(); // invalid
}

In your case use can use generics.

public addRow(row: Row): boolean {
  let constraints: unknown = getConstraints(this.config); // unknown fits too
  let table: any = localStorage.getItem(row.tableName);
  let rowData = {};
}

public getData<T>( // <- T, when we don't know type.
  table_name: string,   entity_name?: string,
  entity_value?: T
): boolean {
  let tableRecord: object[] = [];
  let allTableData: Array<object> = JSON.parse(
    localStorage.getItem(table_name) || ""
  ).data;
}

public deleteRecord<T extends string>( // <- T, when we don't know type.
  table_name: string,
  entity_name: string,
  entity_value: T
): boolean {
  return !!entity_value; // works
}

public updateRecord<T, D extends object>( // <- T, D, when we don't know type.
 table_name: string,
 entity_name: string,
 entity_value: T,
 updation_data: D
): boolean {
  if (typeof entity_name === 'string) {
    // now we know it's a string.
  }
  return !entity_value || !updation_data; // works
}
satanTime
  • 12,631
  • 1
  • 25
  • 73
  • is this type more efficient than interface? –  May 27 '20 at 11:14
  • depends on what you needs. The goal is to have all variables typed, if you don't know its type - try to use generic or unknown instead of any. A type def is more extensible than an interface def. An interface works for objects and classes, a type works for everything including primitives, unions and infers. – satanTime May 27 '20 at 11:16
  • Like that: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgLIE8CS5rycgbwChllIBnMALmUqlAHMBuIgXyKNElkRQwEEoUOOmQQAHpBAATcskHD0AHgzZueCAD5CJZGw4IA9iErI4QkTQEXRAXmQBtAhWrIA5AEYATAGY3rAF0mIA – satanTime May 27 '20 at 12:21
  • Try like that `function getConstraints(config: Config): Array {`. – satanTime May 27 '20 at 13:18
  • actually i just want the any type changed to an interface –  May 27 '20 at 13:21
  • Unfortunately, it's not clear what you want to do. Might you show what you have and what you want to achieve? – satanTime May 27 '20 at 13:22
0

You can use typescript type definition. e.g. if u have multiple types for a variable, you can create a single type as:

type Foo = Bar1 | Bar2 | Bar3

where Bar1, Bar2, Bar3 can be different interfaces or even types.

Vishal Sharma
  • 316
  • 1
  • 8