草庐IT

make_new_type

全部标签

javascript - typescript TS2322 : Type 'typeof Foo' is not assignable to type 'IFoo'

我正在尝试使用ES2015模块语法和TypeScript组合一些类。每个类在.d.ts文件中实现一个接口(interface)。这是问题的MWE。在.d.ts文件中我有:interfaceIBar{foo:IFoo;//...}interfaceIFoo{someFunction():void;//...}我的导出是://file:foo.tsexportdefaultclassFooimplementsIFoo{someFunction():void{}//...}//noerrorsyet.我的导入是:importFoofrom"./foo";exportclassBarimple

javascript - 在 javascript 中强制执行 new 的模式

我一直在阅读StoyanStefanov的JavaScriptPatterns一书,其中一种为构造函数强制使用new运算符的模式是这样的functionWaffle(){if(!(thisinstanceofWaffle)){returnnewWaffle();}this.tastes="yummy";}Waffle.prototype.wantAnother=true;以这种方式编写时,您可以通过以下方式之一调用Wafflevarfirst=newWaffle(),second=Waffle();我认为这是一个有用的功能,不确定它是否会在未来的ecma/javascript版本中实现

javascript - JS 关联数组 : add new pair

我在JS中有一个关联数组。vararray={'one':'first','two':'second','three':'third'};如何在其中添加新对 最佳答案 array['newpair']='newvalue';或array.newpair='newvalue';Thisisquiteadecentreadonthesubject. 关于javascript-JS关联数组:addnewpair,我们在StackOverflow上找到一个类似的问题:

javascript - Angular 力矩 : Moment Timezone has no data for America/New_York

从服务器收到的日期是UTC时区,我需要将其转换为特定时区,例如:America/New_York。以下是相同的代码但是在这样做时我得到了以下错误:MomentTimezonehasnodataforAmerica/New_York.Seehttp://momentjs.com/timezone/docs/#/data-loading/.但是America/New_York是moment的已知时区,但它仍然要求我添加时区。 最佳答案 您需要加载以下所有内容:时刻时刻时区moment-timezone的时区数据Angular矩关于mom

javascript - 流: Dynamically generate string union types?

假设我有一个流类型Suit,我想将它组合成另一种名为Card的类型。//types.jstypeSuit=|"Diamonds"|"Clubs"|"Hearts"|"Spades";typeCard={...suit:Suit,...}与其直接在suit.js中对Suit字符串进行硬编码,不如根据JavaScript原语(数组)动态生成Suit类型?说...//constants.jsconstSUITS=['Diamonds','Clubs','Hearts','Spades'];通过这种方式,西装只需定义一次,并且可以在JavaScript结构中定义,该结构将在应用程序的其他部分中

javascript - 如何撤消 "Meteor.publish"和撤消 "new Meteor.Collection"

我看到在发布时,collection._connection.publish_handlers被填充,collection._connection.method_handlers也被填充,可能还有其他区域。我想通过完全删除对该集合及其发布的引用来基本上清理内存。基本上,应用程序的每个用户都有该用户的收藏列表。有一个看起来像这样的发布功能,供用户获取他们的收藏列表:Meteor.publish('users_collections',function(){varself=this;varhandle=UsersCollections.find({ownerId:self.userId})

javascript - 巴别塔 v6 : How/Can I write a plugin that adds a new syntax (ie a new operator)?

Note:IfoundthisquestiononBabelissuetracker(https://phabricator.babeljs.io/T2653)anditwasrejected,butAFAIKitsauthordidnotaskedithere.我检查了像packages/babel-plugin-syntax-do-expressions这样的Babel插件似乎这些ES6+新语法/运算符实际上根本没有在插件中定义,而是implementedinBabylon并且只是toggledon通过这些插件。在newestblogpost中留下声明“开发人员已经构建了从调试工具

javascript - `new` 运算符如何能够覆盖硬绑定(bind),在 Function.prototype.bind(..)

这是一个纯粹的理论问题。我从“你不懂js”中学习javascript,我一直卡在JS中bind函数的实现上。考虑以下代码:functionfoo(something){this.a=something;}varobj1={};varbar=foo.bind(obj1);bar(2);console.log(obj1.a);//2varbaz=newbar(3);console.log(obj1.a);//2console.log(baz.a);//3在上面的代码片段中,我们将foo()绑定(bind)到obj1,所以foo()中的this>属于obj1,这就是当我们调用bar(2)时o

javascript - Angular 2 : Type 'Subscription' is not assignable to type

我创建了一个非常小的应用程序来从json文件中获取国家并将其绑定(bind)到下拉列表。countries.jsonexportclassCountry{id:number;name:string;}factory.service.tsimport{Injectable}from'@angular/core';import{Http,Response}from'@angular/http';import{Observable}from'rxjs/Observable';import{Country}from'./shared/country';@Injectable()exportcl

javascript - "Resource interpreted as script but transferred with MIME type text/html."

很抱歉,如果这实际上是重复的,但我还没有设法找到我的问题的答案。我使用jQuery的$.getScript加载脚本。但它会导致以下错误:ResourceinterpretedasscriptbuttransferredwithMIMEtypetext/html.该问题仅在MacOS下的Safari中出现如果查看从服务器收到的header,它们包含Content-Type:application/x-javascript,所以我真的不明白问题出在哪里。 最佳答案 Resourceinterpretedasscriptbuttransf