![]() ![]() In addition to describing an object with properties, interfaces are also capable of describing function types. Interfaces are capable of describing the wide range of shapes that JavaScript objects can take. In this instance, if it’s okay to pass an object with both a color or colour property to createSquare, you should fix up the definition of SquareConfig to reflect that. That means if you’re running into excess property checking problems for something like option bags, you might need to revise some of your type declarations. Keep in mind that for simple code like above, you probably shouldn’t be trying to “get around” these checks.įor more complex object literals that have methods and hold state, you might need to keep these techniques in mind, but a majority of excess property errors are actually bugs. Let mySquare = createSquare (' has no properties in common with type 'SquareConfig'. These optional properties are popular when creating patterns like “option bags” where you pass an object to a function that only has a couple of properties filled in. Some exist under certain conditions or may not be there at all. Not all properties of an interface may be required. It’s worth pointing out that the type checker does not require that these properties come in any sort of order, only that the properties the interface requires are present and have the required type. ![]() ![]() If the object we pass to the function meets the requirements listed, then it’s allowed. Notice we didn’t have to explicitly say that the object we pass to printLabel implements this interface like we might have to in other languages. It still represents having a single property called label that is of type string. The interface LabeledValue is a name we can now use to describe the requirement in the previous example. ![]()
0 Comments
Leave a Reply. |