并且尝试定义一个
interface
接口,在其中用计算属性:
export interface SelectAStackType {
[FieldsMap.stack]: "",
[FieldsMap.name]: ""
很遗憾,TypeScript 给我报了一个错误:A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1169)
不过同样有提示说使用计算属性必须要用字面量类型或者唯一的 symbol 类型。
其实这个很好理解,这样定义主要是防止我们在中途去篡改对象里面的值:
const PROPS = {
VALUE: 'value'
PROPS.VALUE = 'aha!';
export default class Main extends Vue {
readonly [PROPS.VALUE]: string
那么引用一个随时都可能的值作为属性,那么就有可能出现找不到属性的情况,因为通过定义 字面量类型 或者 唯一的 symbol 类型 去确保这个值不会被改变。
因为,我们两种解决方案:
- 定义唯一的 symbol 类型:
const level: unique symbol = Symbol();
interface MyInterface {
[level]?: string;
- 定义为字面量类型。要把一个对象变量字面量类型,我们需要类型推断
as
的帮助
export const FieldsMap = {
stack: "Stack" as "Stack",
name: "Name" as "Name",
也可以使用 const
推断,一步到位:
export const FieldsMap = {
stack: "Stack",
name: "Name",
} as const;
[1] A computed property name in a class property declaration must refer to an expression whose type is a literal type or a ‘unique symbol’ type. #4605
[2] Literal Types
解决 TypeScript 报错:A computed property name in an interface must refer to an expression whose type ...
并且尝试定义一个interface不过同样有提示说使用计算属性必须要用或者。那么引用一个随时都可能的值作为属性,那么就有可能出现找不到属性的情况,因为通过定义或者去确保这个值不会被改变。as也可以使用const我是Pandy,一个喜欢英语的程序猿👨💻如果你挣扎于看不懂英语官网,或者对英语感冒的话,可以关注公众号哦,我们加入一起充电英语🔌。......
TypeScript执行和针对Node.js的REPL,具有源映射支持。 使用typescript@>=2.7 。
实验性ESM支持
本机ESM支持目前处于试验阶段。 有关用法,限制和提供反馈,请参阅 。
# Locally in your project.
npm install -D typescript
npm install -D ts-node
# Or globally with TypeScript.
npm install -g typescript
npm install -g ts-node
提示:在本地安装模块可让您通过package.json控制和共享版本。 TS节点在检查相对于其自身的安装之前,将始终从cwd解析编译器。
# Execute a script as `node` + `tsc`.
ts-node script.ts
# Starts a TypeScript REPL.
ts-node
# Execute code with TypeScript.
ts-node -e ' console.log("Hello, world
观察员档案活动和控制权错误ademas crea el archivo js al cambiarlo
tsc --watch |archivo|
普通proyecto打字稿Esto Crea Un tsconfig.json
tsc --init
配置文件没有配置文件
Mixins (由提供的名为mixins的辅助函数)
也可以看看
@Prop(options: (PropOptions | Constructor[] | Constructor) = {})装饰器
import { Vue , Component , Prop } from 'vue-property-decorator'
@ Compone
import { List } from 'linqts' ;
const arr = new List < number> ( [ 1 , 2 , 3 , 4 , 5 ] )
. Where ( x => x > 3 )
. Select ( y => y * 2 )
. ToArray ( ) ; // > [8, 10]
const query = people . Join ( pets ,
person => person ,
pet => pet . Owner ,
<div class="home">
<img alt="Vue logo" src="../assets/logo.png">
<HelloWorld ref="helloComp" msg="Welcome to Your Vue.js + TypeScript App" @change-data="changeData"/>
</template>
code 类型 英文描述 中文描述
1002 错误 Unterminated string literal. 未终止的字符串文本。
1003 错误 Identifier expected. 应为标识符。
1005 错误 '{0}' expected. 应为“{0}”。
1006 错误 A file cannot have a reference to itself. 文件不能引用自身。
1009...
背景:当我们在react中封装组件并复用时,会传不同的props,那么自然有些props是不需要传的,所以在定义interface的时候需要加个问号。但这也就引来了标题的报错!
模拟一下报错代码:
// 父组件
const handleVisible () => {
export default function Father(props: IProps) {
return (
<Child/>
<Child handleVisible={handleVisibl
文章目录接口的作用:设置接口可选属性:额外属性检查:设置接口只读属性:函数类型接口:可索引类型接口:
接口的作用:
接口,英文:interface,其作用可以简单的理解为:为我们的代码提供一种约定。
在Typescript中是这么描述的:
TypeScript的核心原则之一是对值所具有的结构进行类型检查。它有时被称做“鸭式辨型法”或“结构性子类型化”。
在TypeScript里,接口的作用就是为这些类型命名和为你的代码或第三方代码定义契约。
举个例子:
// 定义接口 Person
interfac
vue typescript 项目 初始化报错 fatal: not a git repository (or any of the parent directories): .git husky - git command not found, skipping install
这个错误提示表明你的项目在初始化的时候,git 仓库没有被正确地创建,因此 husky 无法正常安装。你可以尝试以下步骤:
1. 确认你的项目根目录下是否有 .git 文件夹。如果没有,请使用 git init 命令初始化一个 git 仓库。
2. 如果你已经正确地创建了 git 仓库,但 husky 仍然无法安装,那么你可以尝试按照以下步骤解决问题:
- 确认你的系统中是否已经正确地安装了 git。你可以在命令行中输入 git --version 来检查。
- 确认你的项目中是否安装了 husky。你可以在 package.json 文件中查看 dependencies 和 devDependencies 部分,确认是否存在 husky。
- 如果 husky 已经被正确地安装,那么你可以尝试重新安装 husky。你可以使用以下命令:
npm uninstall husky
npm install husky --save-dev
如果你已经尝试了以上操作,但问题仍然存在,那么你可以尝试在 Vue CLI 中使用 TypeScript 模板来创建你的项目,这样可以避免一些常见的问题。
CSDN-Ada助手: