Questions tagged «typescript2.0»

2
打字稿中的记录类型是什么?
Record<K, T>Typescript是什么意思? Typescript 2.1引入了该Record类型,并在一个示例中进行了描述: // For every properties K of type T, transform it to U function mapObject<K extends string, T, U>(obj: Record<K, T>, f: (x: T) => U): Record<K, U> 见打字稿2.1 而高级类型页提到Record的映射类型旁边标题下Readonly,Partial和Pick,这似乎是它的定义: type Record<K extends string, T> = { [P in K]: T; } 只读,部分和选择是同态的,而记录不是同态的。记录不是同态的一个线索是它不需要输入类型来复制以下属性: type ThreeStringProps = Record<'prop1' …

12
TypeScript和React-子类型?
我有一个非常简单的功能组件,如下所示: import * as React from 'react'; export interface AuxProps { children: React.ReactNode } const aux = (props: AuxProps) => props.children; export default aux; 还有另一个组件: import * as React from "react"; export interface LayoutProps { children: React.ReactNode } const layout = (props: LayoutProps) => ( <Aux> <div>Toolbar, SideDrawer, Backdrop</div> <main> …

1
'this'隐式具有类型'any',因为它没有类型注释
在中启用noImplicitThis时tsconfig.json,以下代码会出现此错误: 'this' implicitly has type 'any' because it does not have a type annotation. class Foo implements EventEmitter { on(name: string, fn: Function) { } emit(name: string) { } } const foo = new Foo(); foo.on('error', function(err: any) { console.log(err); this.emit('end'); // error: `this` implicitly has type `any` }); 将类型添加this到回调参数会导致相同的错误: …

5
TypeScript中的clarify关键字的用途
declare关键字的目的是什么? type Callback = (err: Error | String, data: Array<CalledBackData>) => void; 与 declare type Callback = (err: Error | String, data:Array<CalledBackData>) => void; 找不到说明declareTS中关键字目的的文档。

1
如何为TypeScript配置自定义全局接口(.d.ts文件)?
我目前正在研究一个使用Webpack2和TypeScript的ReactJS项目。一切工作都与一件事情完全分开-我找不到一种方法来将自己编写的接口移动到单独的文件中,以使它们对整个应用程序可见。 出于原型目的,我最初在使用它们的文件中定义了接口,但最终我开始添加多个类中需要的一些接口,这就是所有问题开始的时候。无论我对我进行了什么更改tsconfig.json,无论我将文件放在哪里,IDE和Webpack都抱怨找不到名称(“找不到名称'IMyInterface'”)。 这是我当前的tsconfig.json文件: { "compilerOptions": { "baseUrl": "src", "outDir": "build/dist", "module": "commonjs", "target": "es5", "lib": [ "es6", "dom" ], "typeRoots": [ "./node_modules/@types", "./typings" ], "sourceMap": true, "allowJs": true, "jsx": "react", "moduleResolution": "node", "rootDir": "src", "forceConsistentCasingInFileNames": true, "noImplicitReturns": true, "noImplicitThis": true, "noImplicitAny": false, "strictNullChecks": true, "suppressImplicitAnyIndexErrors": true, "noUnusedLocals": true }, "exclude": …

2
打字稿找不到名称窗口或文档
无论哪种情况: document.getElementById('body'); // or window.document.getElementById('body'); 我懂了 error TS2304: Cannot find name 'window'. 我是否缺少tsconfig.json应安装的定义文件? 我在跑步tsc和跑步时收到消息vscode tsconfig.json: { "compilerOptions": { "allowJs": true, "emitDecoratorMetadata": true, "experimentalDecorators": true, "jsx": "react", "module": "commonjs", "moduleResolution": "node", "noEmitOnError": true, "noImplicitAny": false, "sourceMap": true, "suppressImplicitAnyIndexErrors": true, "target": "ES2016", "typeRoots": [ "node_modules/@types/", "typings/index.d.ts" ] }, "exclude": [ "node_modules", "**/*-aot.ts" …

6
如何使用jsdom和typescript防止“类型'Global'上不存在属性'...'?
我尝试将现有项目转换为使用Typescript,但是在测试设置中却遇到了问题。 我有一个用于测试的安装文件,用于设置jsdom,以便我所有的DOM交互代码都可以在测试期间使用。使用Typescript(带有摩卡的ts节点),我总是会收到这样的错误: Property 'window' does not exist on type 'Global'. 为了防止这种情况,我尝试像这样修补NodeJS.Global接口: declare namespace NodeJS{ interface Global { document: Document; window: Window; navigator: Navigator; } } 但这并没有改变任何东西。 如何在NodeJS全局变量上启用这些浏览器属性? 附加功能: 这是我的摩卡咖啡setup.ts: import { jsdom, changeURL } from 'jsdom'; const exposedProperties = ['window', 'navigator', 'document']; global.document = jsdom(''); global.window = global.document.defaultView; Object.keys(global.document.defaultView).forEach((property) => …
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.