JavaScript/typescript:修订间差异

来自康健生活
Kangkk​(留言 | 贡献)
Kangkk​(留言 | 贡献)
 
(未显示同一用户的22个中间版本)
第1行: 第1行:
== 引言 ==
== 引言 ==
{{shc|<syntaxhighlight lang="bash">
npm i -g typescript</syntaxhighlight>|$|/home/kangkk}}
初始化
{{shc|<syntaxhighlight lang="bash">
tsc --init</syntaxhighlight>|$|/home/kangkk}}
== 类型声明 ==
{| class="wikitable"
|-
| [[/数组|数组]] || 示例 || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
== 类型 ==
== 类型 ==
[[/类型|类型]]
[[/类型|类型]]
{| class="wikitable"
|-
| [[/Enum|枚举 enum]] || 示例 || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
== 断言 ==
[[/断言|断言]]
== 关键字 ==
{| class="wikitable"
|-
|  [[/interface|interface]] || [[/type|type]] ||
|-
| [[/回调函数|回调函数]] || --strict || [[/类型收窄|类型收窄]]
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
== 操作符 ==
{| class="wikitable"
|-
| [[/typeof|typeof]] || 示例 || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
== 工具类型 ==
=== 高级类型 ===
{| class="wikitable"
|-
| [[/Pick|Pick]] || [[/Partial|Partial]] || [[/Record|Record]] || [[/Readonly|Readonly]] || [[/Required|Required]]
|}
=== 条件类型 ===
<syntaxhighlight lang="javascript">
type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">;  // "b" | "d"
type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">;  // "a" | "c"
type T02 = Exclude<string | number | (() => void), Function>;  // string | number
type T03 = Extract<string | number | (() => void), Function>;  // () => void
type T04 = NonNullable<string | number | undefined>;  // string | number
type T05 = NonNullable<(() => string) | string[] | null | undefined>;  // (() => string) | string[]
function f1(s: string) {
    return { a: 1, b: s };
}
class C {
    x = 0;
    y = 0;
}
type T10 = ReturnType<() => string>;  // string
type T11 = ReturnType<(s: string) => void>;  // void
type T12 = ReturnType<(<T>() => T)>;  // {}
type T13 = ReturnType<(<T extends U, U extends number[]>() => T)>;  // number[]
type T14 = ReturnType<typeof f1>;  // { a: number, b: string }
type T15 = ReturnType<any>;  // any
type T16 = ReturnType<never>;  // any
type T17 = ReturnType<string>;  // Error
type T18 = ReturnType<Function>;  // Error
type T20 = InstanceType<typeof C>;  // C
type T21 = InstanceType<any>;  // any
type T22 = InstanceType<never>;  // any
type T23 = InstanceType<string>;  // Error
type T24 = InstanceType<Function>;  // Error</syntaxhighlight>


== eslint ==
== eslint ==
第42行: 第134行:
     }
     }
};</syntaxhighlight>}}
};</syntaxhighlight>}}
== 其他 ==
{| class="wikitable"
|-
| [[/as_const|as const]] || [[/断言|断言]] || 示例
|-
| 示例 || 示例 || 示例
|-
| 示例 || 示例 || 示例
|}
== 标题文本 ==
api-extractor是辅助打包 TypeScript 类型系统的工具
{{shc|<syntaxhighlight lang="bash">
pnpm add @microsoft/api-extractor -g
api-extractor init</syntaxhighlight>|$|/home/kangkk}}
== 规范 ==
* [[/lib_es5_d_ts|lib.es5.d.ts]]
== error ==
== error ==
* TypeError: The 'request' argument must be string. Received type undefined
* TypeError: The 'request' argument must be string. Received type undefined
* Argument of type 'string | null | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'
* Argument of type 'string | null | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'
* TS2339: Property '$axios' does not exist on type 'NetworkAuth'.
* TS2339: Property '$axios' does not exist on type 'NetworkAuth'.
* Error:(7, 40) TS2691: An import path cannot end with a '.ts' extension. Consider importing '@@/api/httpInterface' instead.
* Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
: <syntaxhighlight lang="javascript">
// 添加 export {};
// 指定文件为模板
// 用于消除报错 Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
export {};
declare global {
  interface Window {
    run: (sketchName: string, showGroup: boolean) => void
    updateProgress: (progress: string) => void
    onUploadStart: () => void
    onUploadFinish: () => void
  }
}
</syntaxhighlight>
* react: Cannot find module './header.less' or its corresponding type declarations.
: <syntaxhighlight lang="javascript">
declare module "*.less" {
  const content: { [className: string]: string };
  export default content;
}
</syntaxhighlight>
[[/error|error]]

2025年7月5日 (六) 05:13的最新版本

引言

/home/kangkk
npm i -g typescript

初始化

/home/kangkk
tsc --init

类型声明

数组 示例 示例
示例 示例 示例
示例 示例 示例

类型

类型

枚举 enum 示例 示例
示例 示例 示例
示例 示例 示例

断言

断言

关键字

interface type
回调函数 --strict 类型收窄
示例 示例 示例
示例 示例 示例

操作符

typeof 示例 示例
示例 示例 示例
示例 示例 示例

工具类型

高级类型

Pick Partial Record Readonly Required

条件类型

type T00 = Exclude<"a" | "b" | "c" | "d", "a" | "c" | "f">;  // "b" | "d"
type T01 = Extract<"a" | "b" | "c" | "d", "a" | "c" | "f">;  // "a" | "c"

type T02 = Exclude<string | number | (() => void), Function>;  // string | number
type T03 = Extract<string | number | (() => void), Function>;  // () => void

type T04 = NonNullable<string | number | undefined>;  // string | number
type T05 = NonNullable<(() => string) | string[] | null | undefined>;  // (() => string) | string[]

function f1(s: string) {
    return { a: 1, b: s };
}

class C {
    x = 0;
    y = 0;
}

type T10 = ReturnType<() => string>;  // string
type T11 = ReturnType<(s: string) => void>;  // void
type T12 = ReturnType<(<T>() => T)>;  // {}
type T13 = ReturnType<(<T extends U, U extends number[]>() => T)>;  // number[]
type T14 = ReturnType<typeof f1>;  // { a: number, b: string }
type T15 = ReturnType<any>;  // any
type T16 = ReturnType<never>;  // any
type T17 = ReturnType<string>;  // Error
type T18 = ReturnType<Function>;  // Error

type T20 = InstanceType<typeof C>;  // C
type T21 = InstanceType<any>;  // any
type T22 = InstanceType<never>;  // any
type T23 = InstanceType<string>;  // Error
type T24 = InstanceType<Function>;  // Error

eslint

 
npm install eslint typescript --save-dev
npm install --save-dev @typescript-eslint/parser
npm install --save-dev @typescript-eslint/eslint-plugin

配置

AlloyTeam ESLint

 
npm install --save-dev eslint-config-alloy

部署配置

.eslintrc.js
module.exports = {
    extends: [
        'alloy',
        'alloy/typescript',
    ],
    env: {
        // 您的环境变量(包含多个预定义的全局变量)
        // Your environments (which contains several predefined global variables)
        //
        // browser: true,
        // node: true,
        // mocha: true,
        // jest: true,
        // jquery: true
    },
    globals: {
        // 您的全局变量(设置为 false 表示它不允许被重新赋值)
        // Your global variables (setting to false means it's not allowed to be reassigned)
        //
        // myGlobal: false
    },
    rules: {
        // 自定义您的规则
        // Customize your rules
    }
};

其他

as const 断言 示例
示例 示例 示例
示例 示例 示例

标题文本

api-extractor是辅助打包 TypeScript 类型系统的工具

/home/kangkk
pnpm add @microsoft/api-extractor -g
api-extractor init

规范

error

  • TypeError: The 'request' argument must be string. Received type undefined
  • Argument of type 'string | null | undefined' is not assignable to parameter of type 'string'. Type 'undefined' is not assignable to type 'string'
  • TS2339: Property '$axios' does not exist on type 'NetworkAuth'.
  • Error:(7, 40) TS2691: An import path cannot end with a '.ts' extension. Consider importing '@@/api/httpInterface' instead.
  • Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
// 添加 export {};
// 指定文件为模板
// 用于消除报错 Augmentations for the global scope can only be directly nested in external modules or ambient module declarations.
export {};

declare global {
  interface Window {
    run: (sketchName: string, showGroup: boolean) => void
    updateProgress: (progress: string) => void
    onUploadStart: () => void
    onUploadFinish: () => void
  }
}
  • react: Cannot find module './header.less' or its corresponding type declarations.
declare module "*.less" {
  const content: { [className: string]: string };
  export default content;
}

error