Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between 'interface' and 'declare interface'

Tags:

typescript

What is the difference (if any) between:

declare interface SomeInterface {     //members here } 

and:

interface SomeInterface {     //members here } 

?

like image 374
Rapid Here Avatar asked Jul 31 '16 10:07

Rapid Here


People also ask

What is declare interface?

Declaring InterfacesAn interface adds the functionality of strong type checking for your functions, variables, or the class that is implementing the interface. Interfaces make sure that everything is implemented as expected.

What's the difference between interface and type?

Key Differences between TypeScript type vs interface Whereas interfaces are defined as a declaration of the only object type, which means the interfaces are restricted to only object type and do not support any other type for declaration. But we can say that interfaces have more capabilities than types in typescript.

Should I use interface or type?

Interface work better with objects and method objects, and types are better to work with functions, complex types, etc. You should not start to use one and delete the other.

What is the difference between interface and class in TypeScript?

TypeScript class vs.Classes are the fundamental entities used to create reusable components. It is a group of objects which have common properties. It can contain properties like fields, methods, constructors, etc. An Interface defines a structure which acts as a contract in our application.


1 Answers

declare keyword is usually used in type definitions to describe existing classes or variables that are defined externally in JavaScript code.

There's no difference between declare interface and interface because:

  • there's no code generation for interfaces and they exist only in Typescript code so you can not declare interface that's defined in JavaScript code;
  • interface in Typescript by its nature is declaration only, it has no method bodies, properties values, etc. so both declare interface and interface are syntactically equal.
like image 113
mixel Avatar answered Sep 30 '22 19:09

mixel