Were going to say that the name of the Pizza will be of type string and were also going to say that our pizzas are going to have some available sizes - such as 'small', 'medium' or 'large'. What were going to do is use name and sizes, this is the shorthand syntax for creating object literals under the same name as the variables that we want to pass in. So were going to say that we want to create the pizza, we need to pass in a name and the sizes that this pizza is available in. Now its a guarantee that our function will return us what wed like, providing the implementation was correct: What we actually want to do is create an interface instead of a type as it is a preferred approach when were dealing with more complex data structures or data sets. In our case were using this pizza variable and were saying that at some point this Pizza is then going to be of type pizza. Please refresh this page to activate it. So what were going to create is a function called createPizza that will accept a name: string and some sizes: string[]. Directives, simple right? On the outside they look simple, but even skilled Angular devs havent grasped every concept in this eBook. Lets create a Pizzas interface which has a data property which will be made up of a Pizza array Pizza[]. Wrong! We would see something different we would see type Pizza = {name: string; sizes: string[];} so the type Pizza equals an object literal. If you were to hover over the pizza variable you would see its of type pizza let pizza: Pizza - but were not 100% sure that our createPizza function returns us a pizza. A newer version of this site just became available. To start, we will change our type Pizza to interface Pizza: Theres one interesting thing here, we do not need the equals = operator to assign the type a value, as interface is a special TypeScript type and keyword. Heres where were at so far: It might be that in a pizza restaurant they have different pizzas or different bases and theyre only available in certain sizes - but when we create a pizza we want to supply these defaults as the arguments. This is pretty much what we need to know for the basics of learning what an interface is. Lets assume that this is an asynchronous request and inside some other function we are then going to assign the result. This is how you can combine different interfaces, and the same applies to using the type keyword, however we see some additional benefits by using an interface. We can then later reassign the value. Cookies are used to analyze traffic and optimize experience. Lets now learn about TypeScripts interface. These sizes are all going to be a string array string[]. The main purpose here is to start thinking about our interfaces! Lets assume that youre using an API at this point where youd go off to the backend, pass some JSON and then get back from the server a newly created pizza with a dynamic ID. Join 78,277 developers pushing their limits. The most important thing to remember is an interface is typically the preferred way that we can create a contract between some kind of variable and its data, and the shape of the data, that we are essentially telling TypeScript is what it looks like. Tips and tricks, motivation, courses and exclusive discounts. Another small difference between types and interfaces. With interfaces we can do some interesting things because with object-oriented programming we can do things such as extend particular classes and we dont get this benefit with simply a type - but we do with an interface! Using the this keyword in JavaScript often trips developers up, whether theyre a beginner or more experienced. TypeScript allows us to not only create individual types, but combine them to create more powerful use cases and completeness. Premium TypeScript courses to skyrocket your skills to the top. BONUS: Interfaces go nicely with classes, and there is a lot of overlap and confusion. This is known as an inferred type, where we do not describe the type but let TypeScript figure it out for itself. Many developers are confused when choosing between a TypeScript interface or a type. Lets remove the deal: true and see what we have. Weve put together a super resource on answering the question Classes versus Interfaces. If we were to assign something else (for example if we try to add deal: true) things will break: When we hover over this we will see an error - wed expect this with a type as well because a type will also pick up on these errors. Using Enums in TypeScript is a great way to access particular parameters that are meant to be shared across multiple files, for example access leve Interfaces and Types can get messy, but with Mapped Types well learn how to use Pick to simplify your codebase and keep it clean. Lets start by creating our custom type, which is going to be of an object nature. If we were to change this to a type, assign it, and hover over it. A TypeScript Interface is like a more powerful type - so to get a better understanding of interfaces we are going to start off by creating a type and then refactoring it to use an interface. What we could do now is add Pizza as a type to our function and say that the pizza is going to be returned! An interface, much like a type, also creates that contractual agreement between perhaps a variable or a data structure. So name is the same as name: name. When we go to use an interface instead of a type, if you hover over interface Pizza it says interface Pizza, which is a token TypeScript will use if and when we write any code that may produce a compilation error. Its a special type in TypesScript and it allows us to essentially define the structure or a shape of a particular object/data structure. Now we want to create a pizza object and mark it upfront as a type of Pizza.
ページが見つかりませんでした – MuFOH