打字基本要求‘’

    科技2025-02-25  14

    打字基本要求‘’

    Typescript is a language that complies to Javascript. The benefit of using Typescript is that it has type inference, meaning it can infer what type of data is being used. By using Typescript we are able to define what type of data a function or component is expecting to receive or return, and Typescript identifies when the type doesn’t match what it was expecting, then lets you know when you’ve made a type-related mistake by providing a helpful error message when type-related mistakes are made. This reduces the need to debug when you program is not working as expected.

    Typescript是一种符合Javascript的语言。 使用Typescript的好处是它具有类型推断功能,这意味着它可以推断正在使用哪种类型的数据。 通过使用Typescript,我们可以定义函数或组件期望接收或返回的数据类型,并且Typescript可以识别何时该类型与期望的类型不匹配,然后让您知道何时创建了类型-与类型相关的错误时,通过提供有用的错误消息来提供相关的错误。 这样可以减少您的程序无法按预期工作时进行调试的需要。

    The type of a variable or function is defined after the variable declaration with a colon and the type, let type: string = “string". When you assign a variable or a function to a type, the returned value must be that type, otherwise Typescript warns you that you have made a mistake.

    在变量声明后用冒号和类型定义变量或函数的类型, let type: string = “string" 。当您将变量或函数分配给类型时,返回值必须是该类型,否则打字稿警告您犯了一个错误。

    In the example above, Typescript was expecting the value of snacks to be an array of strings, but it was assigned a single string. With brackets around the string, the Typescript warning is remedied.

    在上面的示例中,Typescript期望snacks的值是一个字符串数组,但分配给它一个字符串。 在字符串两边使用方括号,可纠正“打字稿”警告。

    打字稿类型 (Typescript Types)

    Typescript has the same types as Javascript, plus a Enum type. Below is an example of each in a simple example.

    Typescript与Javascript具有相同的类型,外加一个Enum类型。 下面是一个简单示例中的每个示例。

    // STRINGS const potatoChips: string = "Potato Chips"; const dip: string = "Dip"; const foodILove = ` I love ${potatoChips} and ${dip}! `; // BOOLEANS let hungry: boolean = true; // NUMBERS // include floating point, hexidecimal, and decimal literals // As of ECMAScript 2015 octal literals and biancy let num: number = 8 + 0xf00d + 0b1010 + 0o744; // ARRAYS let favoriteSnacks: string[] = [ "popcorn", "tacos", "gummy worms", "cheese puffs", ]; let snacks: Array<string> = favoriteSnacks; // TUPLES let meal: [string, object] = ["chips and salsa", { tacos: 3 }]; // OBJECTS // Anything that is non-primative. Not a number, string, boolean, symbol, null, or undefined. const obj: object = new Map(); const hambuger: object = {"bun": 2, "patty": 1, "lettuce": 1, "tomato": 1, "onion": 2, "sauce": "the perfect amount" } //ENUMS enum Snack { Chips, Popcorn, Gummies, Apple, } let snack: Snack = Snack.Popcorn; // ANY let mySnack: any; mySnack = "popcorn" mySnack = 10; mySnack = true; mySnack = {"tasty": true}; // VOID let emptyStomachWarning: void = console.log("Change hungry status to true") // NULL AND UNDEFINED let missedLunch: null = null let exactTimeForSnack: undefined = undefined

    任意种类(Any Type)

    Variables assigned to the any type can have a value of any data type. Any type is great for situations where not all type information is available, or when you are first incorporating Typescript into a project because it essentially allows skipping a type check, as needed, since the value can be any time.

    分配给any类型的变量可以具有任何数据类型的值。 任何类型都适用于并非所有类型信息都可用的情况,或者是您第一次将Typescript合并到项目中时,因为值实际上可以随时随地进行,因此它基本上允许根据需要跳过类型检查。

    let snack: any;snack = "popcorn";snack = 10;snack = true;snack = { tasty: true };

    空或未定义的类型 (Null or Undefined Types)

    Null and Undefined types are not helpful on their own:

    Null和Undefined类型本身没有帮助:

    let missedLunch: null = nulllet exactTimeForSnack: undefined = undefined

    However, they can be helpful in scenarios where a value may be null or undefined, such as:

    但是,它们在值可能为null或undefined情况下可能会有所帮助,例如:

    let todaysLunch: undefined | string = "falafel"let tomorrowsLunch: undefined | string = undefined

    空洞 (Void)

    Void is useful for console.logs or any other function where there is no return value.

    对于console.logs或没有返回值的任何其他函数,Void非常有用。

    let emptyStomachWarning: void = console.log("Change hungry status to true");

    未知 (Unknown)

    Unknown type is used when the type of the variable is unknown, such as dynamic content from the user, or when you want to allow all values into the API.

    当变量的类型未知(例如来自用户的动态内容)或要允许所有值进入API时,将使用未知类型。

    对象: (Objects:)

    Objects are not as simple to type as strings and numbers are, because Typescript wants to know the form of the object. In my example below, I typed filterValues to be an object, however I was getting an error that said “Property ‘label’ does not exist on type ‘object’”

    Objects的键入不像strings和numbers那样简单,因为Typescript想要知道对象的形式。 在下面的示例中,我将filterValues键入为object ,但是却收到一条错误消息,指出“Property 'label' does not exist on type 'object'”

    Even though filterValues was an object type, with a property of label.

    即使filterValues是对象类型,但具有label属性。

    So I created an interface for FilterValues, and set that value to filterValues in the Prop interface.

    因此,我为FilterValues创建了一个接口,并在Prop interface将该值设置为filterValues 。

    类型断言 (Type Assertions)

    There are times when you may know more than Typescript does about the data types, for those situations you are able to tell Typescript, don’t worry just allow ___ type. You can do type assertions with the as-syntax or the angle-bracket syntax as shown below

    有时候您可能比Typescript更了解数据类型,对于那些您可以告诉Typescript的情况,不必担心只允许___类型。 您可以使用as-syntax或angle-bracket syntax键入断言,如下所示

    功能(Functions)

    For functions, types need to be added to the parameters to ensure the values being passed to the function are the types that the function operate on, and a type can be added after the parameters to indicate the type that needs to be returned by the function. In the example below the function returns a console.log which is void type.

    对于函数,需要将类型添加到参数中以确保传递给函数的值是函数在其上操作的类型,并且可以在参数之后添加类型以指示该函数需要返回的类型。 。 在下面的示例中,该函数返回一个void类型的console.log 。

    But if we try to pass the variable doritos as an object type, we get an “Argument of type ‘…’ is not assignable to parameter of type ‘…’” because this eat() function is expecting a string and not an object.

    但是,如果我们尝试将变量doritos作为object类型传递,则会得到“类型'...'的参数不能分配给类型'...'的参数”,因为此eat()函数需要的是string而不是object 。

    打字稿推断类型: (Typescript Infers Types:)

    There are times when Typescript knows the types without them being declared. In the below example the top function does not have it’s return type declared, but when you hover over the IDE provides a message with the return type included.

    有时候,Typescript会知道类型而无需声明它们。 在下面的示例中,顶部函数没有声明其返回类型,但是当您将鼠标悬停在IDE上时,会提供一条包含返回类型的消息。

    可选参数: (Optional Parameters:)

    In Typescript the same number of arguments needs to be given to the function as the parameters defined by the function. If the function is expecting two parameters and you give it one, Typescript will give you a helpful warning, like the one below.

    在Typescript中,需要为函数提供与函数定义的参数相同数量的参数。 如果函数需要两个参数,而您给它一个参数,Typescript会给您一个有用的警告,如下所示。

    You can make parameters optional by adding a ? at the end of the parameter name. In the below example by adding a ? mark after the parameter quantity, the function can be called with and without a quantity.

    您可以通过添加?将参数设为可选? 在参数名称的末尾。 在下面的示例中,添加一个? 在参数quantity后面标记,可以在有和没有quantity调用该函数。

    Default parameters are also treated as optional when they are they follow required parameters.

    当默认参数遵循必需参数时,它们也被视为可选参数。

    翻译自: https://medium.com/@francesca.dreith/typescript-basics-types-and-basic-function-usage-319a4f37c01

    打字基本要求‘’

    相关资源:fastify-typescript-generator:以每个人喜欢的语言打字稿生成新的fastify应用程序,并根据您的项目需求提供各种选项供您选择-源码
    Processed: 0.010, SQL: 8