Hi i have had this question while i was learning the basics of typescripts, i have asked the question on stackoverflow.
https://stackoverflow.com/q/75343408/16057454
#Typescript does not seem to restrict the parameters passed in a constructor
12 messages · Page 1 of 1 (latest)
@humble musk What would be the point of interfaces if every property of the class had to appear on the interface?
The point of implementing an interface - though there's actually fairly little reason to do so - is to say "the class must have at least these properties.
Anything that has a format() method that can be called without arguments and returns a string is a valid HasFormmater. (It doesn't even have to be a class, or use implements HasFormatter - unlike Java)
This may be helpful: https://www.typescriptlang.org/play#example/structural-typing
The Playground lets you write TypeScript or JavaScript online in a safe and sharable way.
@unkempt heath if we define any interface and have an object
const obj1:interface1;
obj1 cannot have any additional property that are not mentioned in the interface.
The Share Code Snippets Tool helps you to easily share code snippets online with your developer friends or colleagues.
@unkempt heath see the above codesnippet
that is only a requirement in the context of literals
interface Foo {
foo: string;
}
// error because we are constructing a literal value on the RHS
const foo1: Foo = { foo: "", bar: 1 };
// ^^^^^^
// Type '{ foo: string; bar: number; }' is not assignable to type 'Foo'.
// Object literal may only specify known properties, and 'bar' does not exist in type 'Foo'.
// implictly has the type { foo: "", bar: 1 }
const foo2 = { foo: "", bar: 1 };
// no error, `foo2` is assignable to `Foo`
const foo3: Foo = foo2;