No. TypeScript Interfaces are not something available at runtime, that is they are completely absent in the generated JavaScript.

Perhaps you meant to use class:

class Test{
    imp(){return 123}
 }
Answer from basarat on Stack Overflow
🌐
DEV Community
dev.to › qpwo › documenting-default-interface-values-in-typescript-or-trying-to-3b01
Documenting default interface values in typescript, or trying to... - DEV Community
May 9, 2022 - What you are concerned with here is having correctly documented default values in function arguments. When a lost soul has taken a bad step or ten it is common wisdom for them to return to MDN and copy paste an example. Perhaps their example, although in javascript and having no authorial intent to be used for generating typescript declaration files, is so perfect that tsc will submit to it.
Discussions

Interface Default Values
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.) This feature would agree with the rest of TypeScript's Design Goals. ... The best syntax would be , This suggests x default value is "y" and when x value is not given "y" is used · export interface ... More on github.com
🌐 github.com
3
July 12, 2023
Allow default methods/instance variables in interfaces
This would be epic to have. Java itself had a problem with forcing the implementing class to implement the abstract methods, and solved it with default interface implementations. Sometimes, multipl... More on github.com
🌐 github.com
3
June 17, 2015
Replacing Inheritance with default interface implementation
Definitely worth something! In fact, if I understand your idea correctly, Kotlin already did it . If a class Xyz has a field b: Base which implements some interface MyInterface, then Xyz can implement the same interface via class Xyz(val b: Base): MyInterface by b { // ... class body } And you can override any interface methods you want in the class using the usual override keyword, just like normal. (Technically, b needn't even be a field; merely a constructor argument, which is useful if all you need it for is the interface implementation) More on reddit.com
🌐 r/ProgrammingLanguages
31
12
July 10, 2024
How to use interface and default parameters together?
I wanna to makeoptionshave defaulte value,like this: · How can I do?Wating online :) More on github.com
🌐 github.com
2
September 16, 2015
🌐
typescriptlang.org
typescriptlang.org › docs › handbook › 2 › objects.html
TypeScript: Documentation - Object Types
In TypeScript, we represent those through object types. ... In all three examples above, we’ve written functions that take objects that contain the property name (which must be a string) and age (which must be a number). We have cheat-sheets available for both type and interface, if you want ...
🌐
EDUCBA
educba.com › home › software development › software development tutorials › typescript tutorial › typescript interface default value
TypeScript Interface Default Value | Learn the Syntax and Examples
April 18, 2023 - To declare an interface, an interface keyword is used. Below syntax shows how to declare an interface in TypeScript− ... Now within this declaration syntax of interface different objects can be defined whose default values are mentioned for example as,
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Tim Mousk
timmousk.com › blog › typescript-interface-default-value
How To Set Up A TypeScript Interface Default Value? – Tim Mouskhelichvili
March 27, 2023 - By using the TypeScript pick utility type, we can select properties from an interface and provide default values for them.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 54979
Interface Default Values · Issue #54979 · microsoft/TypeScript
July 12, 2023 - This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.) This feature would agree with the rest of TypeScript's Design Goals. ... The best syntax would be , This suggests x default value is "y" and when x value is not given "y" is used · export interface AccordionColors { x : string = "y" }
Author   wakaztahir
🌐
Bobby Hadz
bobbyhadz.com › blog › typescript-interface-default-values
How to set up TypeScript interface Default values | bobbyhadz
February 26, 2024 - The function defines the default values for the Person interface and uses the spread syntax (...) to unpack the defaults before unpacking any of the user-provided values.
Find elsewhere
🌐
Delft Stack
delftstack.com › home › howto › typescript › typescript interface default value
How to Interface Default Value in TypeScript | Delft Stack
February 2, 2024 - In this code, the SquareVal takes the return value from the function Square, which has a parameter type of interface SquareConfig, which has an optional value that can be assigned or not depending upon the situation of usage.
🌐
GitHub
github.com › microsoft › TypeScript › issues › 3536
Allow default methods/instance variables in interfaces · Issue #3536 · microsoft/TypeScript
June 17, 2015 - interface IFoo { bar = true; } class Foo {} class Bar implements IFoo {} new Foo().bar; // undefined new Bar().bar; // true ... Needs ProposalThis issue needs a plan that clarifies the finer details of how it could be implemented.This issue needs a plan that clarifies the finer details of how it could be implemented.SuggestionAn idea for TypeScriptAn idea for TypeScript
Author   dead-claudia
🌐
Reddit
reddit.com › r/programminglanguages › replacing inheritance with default interface implementation
r/ProgrammingLanguages on Reddit: Replacing Inheritance with default interface implementation
July 10, 2024 -

I have been thinking about inheritance vs composition a bit lately. A key point of inheritance is implicit reuse.

Lets say we want to extend the behavior of a function on a class A. In a language with inheritance, it is trivial.

class A {
  func doSomething() { }
}

class Ae extends A {
  func doSomething() {
    super.doSomething();
    // we do something else here
  }
}

With composition and an interface we, embed A within Ae and call the method and, within expectation, have the same result

interface I {
  func doSomething();
}

class A implements I {
  func doSomething() { }
}

class Ae implements I {
  A a;
  func doSomething() {
    a.doSomething();
    // do someting else
  }
}

This works great... until we run into issues where the interface I is long and in order to implement it while only modifying one method, we need to write boiler plate in Ae, calling A as we go explicitly. Inheritance eliminates the additional boiler plate (there may be other headaches including private data fields and methods, etc, but lets assume the extension does not need access to that).

Idea: In order to eliminate the need to explicit inheritance, we add language level support for delegates for interfaces.

interface I {
  func doSomething();
  func doSomething2();
}

class A implements I {
  func doSomething() { }
  func doSomething2() { }
}
// All methods in I which are not declared in Ae are then delegated to the
// variable a of type A which implements I. 
class Ae implements I(A a) {
  func doSomething() {
    a.doSomething();
    // do someting else
  }
  // doSomething2 already handled.
}

We achieve the reuse of inheritance without an inheritance hierarchy and implicit composition.

But this is just inheritance?

Its not though. You are only allowed to use as a type an interface or a class, but not subclass from another class. You could chain together composition where a "BASE" class A implements I. Then is modifed by utilizing A as the default implementation for class B for I. Then use class B as default implementation for class C, etc. But the type would be restricted into Interface I, and not any of the "SUB CLASSES". class B is not a type of A nor is class C a type of B or A. They all are only implementing I.

Question:

Is this worth anything or just another shower thought? I am currently working out ideas on how to minimize the use of inheritance over composition without giving up the power that comes from inheritance.

On the side where you need to now forward declare the type as an interface and then write a class against it, there may be an easy way to declare that an interface should be generated from a class, which then can be implemented like any other interface as a language feature. This would add additional features closer to inheritance without inheritance.

Why am I against inheritance?

Inheritance can be difficult? Interfaces are cleaner and easier to use at the expense of more code? Its better to write against an Interface than a Class?

Edit 1:

Both-Personality7664 asked regarding how internal function dependencies within the composed object would be handled.

A possible solution would be how the underlying dispatching works. With a virtual table implementation, the context being handled with the delegate would use a patched virtual table between the outer object and the default implementation. Then the composing object call the outer objects methods instead of its own.

// original idea result since A.func1() calling func2() on A would simply call A.func2()
Ae.func1() -> A.func1() -> A.func2()

// updated with using patched vtable // the table would have the updated methods so we a dispatch on func2() on A would call Ae with func2() instead of A. Ae.func1() -> A.func1() -> Ae.func2()

Edit 2:

Mercerenies pointed out Kotlin has it.

It seems kotlin does have support for this, or at least part of it.

Top answer
1 of 5
11
Definitely worth something! In fact, if I understand your idea correctly, Kotlin already did it . If a class Xyz has a field b: Base which implements some interface MyInterface, then Xyz can implement the same interface via class Xyz(val b: Base): MyInterface by b { // ... class body } And you can override any interface methods you want in the class using the usual override keyword, just like normal. (Technically, b needn't even be a field; merely a constructor argument, which is useful if all you need it for is the interface implementation)
2 of 5
4
There are several approaches for interface "inheritance" that also allow the interfaces to have fields. Basically multiple inheritance without the downsides. The biggest question is always: what if multiple interfaces have the same methods with default implementations? Which one is called? Scala traits (mixins) use linearization: the first mentioned trait is the most concrete and overrides all other implementations of traits coming later. The order matters. Pharo traits work differently: they just don't compile if you have a conflict and you need to explicitly rename one of the methods, override it or hide it. In terms of "automatically forward methods to a member": this is a Smalltalk (Pharo) classic. Instead of failing compilation when a method isn't available, the method (message) is passed to a doesNotUnderstand: method, which can just forward the message to some wrapped object. This works because smalltalk is inherently dynamically typed and reflective.
🌐
Technical Feeder
technicalfeeder.com › 2022 › 08 › typescript-how-to-set-a-default-value-with-interface
TypeScript How to set a default value with Interface | Technical Feeder
October 26, 2022 - const MyInterfaceDefault: MyInterface = { a: 0, b: "default-string", c: null, }; const data1: MyInterface = { ...MyInterfaceDefault, c: "something", }; const data2: MyInterface = { ...MyInterfaceDefault, c: { value: 1 }, }; console.log(data1); // { a: 0, b: 'default-string', c: 'something' } console.log(data2); // { a: 0, b: 'default-string', c: { value: 1 } } Please check the following article if you still don’t know about the three dots called spread operator. TypeScript/JavaScript Spread operator (three dots) The spread operator (three dots) is used to copy an array and expand an array to pass the values to another object or function parameters.
🌐
Graphite
graphite.com › guides › typescript-interfaces
Understanding interfaces in TypeScript
Interfaces themselves do not hold default values in TypeScript; they are strictly for typing. To emulate default values, you might use classes or function parameters with defaults.
🌐
Medium
medium.com › @rare › setting-default-values-in-typescript-ac7f31482996
Setting Default Values in TypeScript | by rare. | Medium
May 14, 2024 - Sometimes it is wise to stick to default values for parameters in TypeScript / JavaScript functions. I mainly use two approaches in my daily life to handle this. ... Lots of little default values to pick from. ... interface Superhero { name?: string; skill?: string; isSmart?: boolean; } const defaultSuperhero: Superhero = { name: 'Unknown Hero', skill: 'Unknown Skill', isSmart: false, }; function describeSuperhero(superhero: Superhero = defaultSuperhero): void { const { name, skill, isSmart } = superhero; console.log(`Superhero: ${name}. Skill: ${skill}. Smart: ${isSmart}`); } // Example usage: describeSuperhero(); // Output: Superhero: Unknown Hero.
🌐
GitHub
github.com › Microsoft › TypeScript › issues › 4819
How to use interface and default parameters together? · Issue #4819 · microsoft/TypeScript
September 16, 2015 - I wanna to makeoptionshave defaulte value,like this: options = { "type": "alnum", "length": null, "min": 0, "max": Infinity, "uppercase": true, "lowercase": true, "transform": null, "valueChange": function(element, value) {} };
Author   ChuanfengLai
🌐
LogRocket
blog.logrocket.com › home › understanding and using interfaces in typescript
Understanding and using interfaces in TypeScript - LogRocket Blog
October 17, 2024 - While interfaces in TypeScript are excellent for defining the shape of objects, they cannot directly specify default values for properties. However, we can use a combination of optional properties and factory functions to achieve a similar effect.
🌐
Squash
squash.io › how-to-set-default-values-in-typescript
How to Set Default Values in TypeScript - Squash Labs
October 14, 2023 - The greet function has a default parameter of type Person, which means if no object is provided when calling the function, it will use the default value of { name: "John Doe", age: 30 }. Related Article: Tutorial: Extending the Window Object ...
🌐
Webdevtutor
webdevtutor.net › blog › typescript-interface-default-function
Exploring TypeScript Interface Default Functions
To define a default function within a TypeScript interface, you can simply specify the function signature along with its default implementation.