Tag: 打字稿

TypeScript中“=>”的含义是什么? (肥箭)

我刚开始学习TypeScript。 而且我看到有很多使用这个sytax =>的代码。 我通过阅读TypeScript Version 1.6规范和一些谷歌做了一些研究。 我还是不明白=>的意思。 对我来说,感觉就像在C ++中的一个指针 。 但是我不能确认。 如果有人能解释下面的例子,那将是很棒的。 谢谢! 以下是我在阅读Typescript规范时find的例子。 : 对象types var MakePoint: () => { x: number; y: number; }; 问题:这段代码在做什么? 用x和y创build一个名为MakePoint的对象,并以数字types填充 ? 这是MakePoint的构造函数还是函数? 函数types function vote(candidate: string, callback: (result: string) => any) { // … } 问题: => any什么意思? 你必须返回stringtypes的结果? 有人可以用适当的例子来解释这些差异或目的吗? 谢谢!

TypeScript编译为一个单独的JS文件

我有一个关于TypeScript的testing项目,代码可以在这里find。 当我使用VS2012创build新项目时,会创build一个app.ts文件。 当我改变它的内容,如链接所示,并添加新模块称为GameModule ,我得到编译错误。 当我',删除app.ts和创buildMain.ts ,一切编译好,但有一个问题 – 只有Main.ts被编译到Main.js , GameModule.ts保持未编译。 我怎样才能让编译器合并一个JS中的所有代码?

使用TypeScript和$ inject机制定义AngularJS指令

最近我开始重新构build我正在使用TypeScript的Angular项目之一。 使用TypeScript类来定义控制器非常方便,并且由于static $inject Array<string>属性,所以可以很好地处理缩小的JavaScript文件。 而且你可以得到相当干净的代码,不需要从类定义中分解angular依赖关系: module app { 'use strict'; export class AppCtrl { static $inject: Array < string > = ['$scope']; constructor(private $scope) { … } } angular.module('myApp', []) .controller('AppCtrl', AppCtrl); } 现在我正在寻找解决scheme来处理指令定义的类似情况。 我发现一个很好的做法来定义指令作为函数: module directives { export function myDirective(toaster): ng.IDirective { return { restrict: 'A', require: ['ngModel'], templateUrl: 'myDirective.html', replace: true, link: (scope: […]

为什么在TypeScript中默认导出界面的限制?

我正在使用TypeScript 1.5testing版,我试图导出一个接口作为默认导出。 以下代码在Visual Studio和WebStorm中都会导致错误: export default interface Foo {…} 但是,下面的代码工作正常: interface Foo {…} export default Foo; 这是devise,这是一个错误,或者我做错了什么? 编辑:谢谢你的回答。 但是,它提出了一个问题,那么使用ES6模块语法导入接口的方式是什么? 这工作: // Foo.ts export interface Foo {} // Bar.ts import {Foo} from 'Foo'; // Notice the curly braces class Bar { constructor(foo:Foo) {} } 但是,既然这样,为什么不允许默认导出并保存大括号? // Foo.ts export default interface Foo {} // Bar.ts import Foo […]

在Typescript中的数组<Type> VS Type

据我所知,一个属性的types可以通过两种方式来定义。 property_name: type 哪里types可以是 Array<string>, ,Array<MyType>, etc. (eg let prop1: Array<string>) 和 string[], MyType[], etc. (eg let prop1: string[]) 这两种情况有什么区别 ? 或者我误解了一些东西(也许是关于铸造中使用的东西?) 编辑,因为问题标记为重复,我知道有任何[]的其他问题,但我仍然看过它发布之前,对我来说这是更多的types'任何'比不同的[] VS < >我问

我如何检查是否提供了可选参数?

给定一个具有可选参数的函数: function DoSomething(a, b?) { /** Implementation */ } 我如何确定是否从函数体内提供了一个可选参数? 目前,我能想到的最好的方法是: typeof b === 'undefined' 但这是一种混乱,不能直接阅读。 由于TypeScript提供了可选的参数支持,我希望它也有一个直观的方法来检查是否提供了一个参数。 如上例所示,我不介意可选参数是否显式设置为undefined或根本没有提供。

如何在容器中放置一个dynamic组件

我想创builddynamic组件并将这些组件的视图插入到容器中。 我认为这可以通过ViewContainerRef来实现。 但我不知道,我们可以得到任何组件的ViewContainerRef ? 如果是的话那怎么样? 我是Angular的新手,如果还有其他好的解决scheme可以处理这种情况,请给我build议。 更新我认为,我非常接近解决scheme。 下面是代码。 app.component.ts import {Component} from '@angular/core'; import {ContainerComponet} from './container.component' @Component({ selector: 'my-app', template: ` <container> </container> `, directives: [ContainerComponet] }) export class AppComponent { constructor() { } } container.component.ts import {Component, ComponentResolver, ViewContainerRef} from '@angular/core' import {controlBoxComponent as controlBox} from './controlBox.component'; @Component({ selector: 'container', template: 'container' }) […]

如何将打字稿添加到现有的Asp.Net MVC项目?

我喜欢typcript背后的想法,但似乎无法弄清楚如何将其包含在ASP.Net MVC项目中。 我已经安装了Visual Studio扩展,但似乎无法find如何添加*.ts文件编译为*.js文件的示例或文档。 编辑:其实,我应该复制的Win8例如.jsproj包括和处理.ts文件的方式? 或者只能用于HTML / JS Win8项目?

在接口定义中可以使用getters / setter吗?

目前, TypeScript不允许在接口中使用get / set方法(访问器)。 例如: interface I { get name():string; } class C implements I { get name():string { return null; } } 此外,TypeScript不允许在类方法中使用数组函数expression式:例如: class C { private _name:string; get name():string => this._name; } 有没有其他方法可以在接口定义上使用getter和setter?

在Angular 2中dynamic加载外部js脚本

我有这个模块将外部库与其他逻辑组件在一起,而不需要将<script>标签直接添加到index.html中: import '../../../path/file.html' //import '../js/file.js' @Component({ selector: 'my-app', template: ` <script src="http://iknow.com/this/does/not/work/either/file.js"></script> <div>Template</div>` }) export class MyAppComponent {…} 我注意到ES6规范的import是静态的,并且在TypeScript转译期间而不是在运行时被解决。 无论如何,使其可configuration,所以file.js将从CDN或本地文件夹加载? 如何让Angular 2dynamic加载脚本?