小编Dav*_*ret的帖子

如何实现打字稿装饰器?

TypeScript 1.5现在有装饰器.

有人可以提供一个简单的例子来演示实现装饰器的正确方法,并描述可能的有效装饰器签名中的参数是什么意思吗?

declare type ClassDecorator = <TFunction extends Function>(target: TFunction) => TFunction | void;
declare type PropertyDecorator = (target: Object, propertyKey: string | symbol) => void;
declare type MethodDecorator = <T>(target: Object, propertyKey: string | symbol, descriptor: TypedPropertyDescriptor<T>) => TypedPropertyDescriptor<T> | void;
declare type ParameterDecorator = (target: Function, propertyKey: string | symbol, parameterIndex: number) => void;
Run Code Online (Sandbox Code Playgroud)

另外,在实现装饰器时是否应该记住哪些最佳实践注意事项?

decorator typescript

198
推荐指数
3
解决办法
7万
查看次数

如何在数组中获取唯一值

如何获取数组中的唯一值列表?我是否总是必须使用第二个数组,或者在JavaScript中是否有与java的hashmap类似的东西?

我将只使用JavaScriptjQuery.不能使用其他库.

javascript jquery

145
推荐指数
14
解决办法
25万
查看次数

.day()使用Moment.js返回错误的某一天

我使用Moment.js来解析字符串并分别获取日,月和年:

var date = moment("12-25-1995", "MM-DD-YYYY");
var day = date.day();        
Run Code Online (Sandbox Code Playgroud)

但是,day它不是25-it 1.什么是正确的API方法?

javascript datetime momentjs

41
推荐指数
3
解决办法
4万
查看次数

tslint中的"no-string-literal"规则是什么?

这里的描述非常简短:

禁止通过字符串文字访问对象.

还有其他文档或示例吗?

typescript tslint

15
推荐指数
1
解决办法
4658
查看次数

为什么在这个例子中使用生成器函数比填充和迭代数组慢?

两个功能的故事

我有一个函数填充数组到指定的值:

function getNumberArray(maxValue) {
    const a = [];

    for (let i = 0; i < maxValue; i++) {
        a.push(i);
    }

    return a;
}
Run Code Online (Sandbox Code Playgroud)

和一个类似的生成器函数,而不是产生每个值:

function* getNumberGenerator(maxValue) {
    for (let i = 0; i < maxValue; i++) {
        yield i;
    }
}
Run Code Online (Sandbox Code Playgroud)

测试跑步者

我已经为这两种情况编写了这个测试:

function runTest(testName, numIterations, funcToTest) {
    console.log(`Running ${testName}...`);
    let dummyCalculation;
    const startTime = Date.now();
    const initialMemory = process.memoryUsage();
    const iterator = funcToTest(numIterations);

    for (let val of iterator) {
        dummyCalculation = numIterations - val;
    }

    const finalMemory = …
Run Code Online (Sandbox Code Playgroud)

javascript generator ecmascript-6

13
推荐指数
3
解决办法
1124
查看次数

在Typescript中为单个行抑制未使用的属性警告

有没有办法让我的代码用ts-node编译,即使我的.ts文件的一行没有未使用的属性警告而没有"noUsedLocals": false在我的tsconfig.json文件中设置?

typescript

13
推荐指数
2
解决办法
8623
查看次数

如何在Windows Phone 8.1/Windows 8.1中为http客户端请求操作设置超时

如何将Timeout属性设置为Windows.Web.Http.HttpClientoperation.我使用的代码示例如下.

public HttpClient httpClient;
public CancellationTokenSource cts;

public void SendRequest(addressUri,postrequestbody)
{
    HttpHelper.CreateHttpClient(ref httpClient);
    cts = new CancellationTokenSource();
    HttpRequestMessage msg = 
        new HttpRequestMessage(new HttpMethod("POST"), 
                               new Uri(addressUri));
    msg.Content = new HttpStringContent(postrequestbody);
    msg.Content.Headers.ContentType = 
        new HttpMediaTypeHeaderValue("application/json");
    HttpResponseMessage response = 
        await httpClient.SendRequestAsync(msg).AsTask();

    if (response.StatusCode == HttpStatusCode.Ok)
    {
    }
}
Run Code Online (Sandbox Code Playgroud)

c# httpclient windows-8.1 windows-phone-8.1 win-universal-app

12
推荐指数
1
解决办法
5887
查看次数

如何键入检查内存中的 TypeScript 代码片段?

我正在我的应用程序Data-Forge Notebook 中实现 TypeScript 支持。

我需要编译、类型检查和评估 TypeScript 代码片段。

编译似乎没有问题,我使用transpileModule如下所示将一段 TS 代码转换为可以评估的 JavaScript 代码:

import { transpileModule, TranspileOptions } from "typescript";

const transpileOptions: TranspileOptions = {
    compilerOptions: {},
    reportDiagnostics: true,
};

const tsCodeSnippet = " /* TS code goes here */ ";
const jsOutput = transpileModule(tsCodeSnippet, transpileOptions);
console.log(JSON.stringify(jsOutput, null, 4));
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试编译有错误的 TS 代码时出现问题。

例如,下面的函数有一个类型错误,但它在没有任何错误诊断的情况下被转换:

function foo(): string {
    return 5;
}
Run Code Online (Sandbox Code Playgroud)

转译很棒,但我也希望能够向我的用户显示错误。

所以我的问题是如何做到这一点,同时还要进行类型检查并为语义错误产生错误?

请注意,我不想将 TypeScript 代码保存到文件中。这对我的应用程序来说将是一个不必要的性能负担。我只想编译和键入保存在内存中的代码片段。

typechecking transpiler typescript typescript-compiler-api

8
推荐指数
2
解决办法
1483
查看次数

Typescript,序列化类对象

如何将这些类序列化为JSON?

从下面的示例中可以看出,JSON.stringify()没有序列化对象Cache_Backend_LocalStorage_Tag内部的列表Cache_Backend_LocalStorage_TagThree.

我错过了什么?

interface Cache_Backend_LocalStorage_Tag_Interface {
    _tag : string;
    _keys : string[];
}

class Cache_Backend_LocalStorage_Tag implements Cache_Backend_LocalStorage_Tag_Interface {

    public _tag : string;
    public _keys : string[];

    constructor(tag : string) {
        this._tag = tag;

        this._keys = [];
    }

    public add(key : string) : void {
        this._keys.push(key);   
    }

    public remove(key : string): boolean {
        // Get the index of the key
        var index = this._keys.indexOf(key, 0);

        // Check if we found the keys index
        if (index …
Run Code Online (Sandbox Code Playgroud)

javascript typescript

7
推荐指数
1
解决办法
8644
查看次数

编译带有浏览器导入的 TypeScript 文件

我的项目中有一个.ts使用导入的文件。当然,这些导入在浏览器中不起作用,因此我想编译我的打字稿文件以使其在浏览器中受支持

{
  "compilerOptions": {
    "noImplicitAny": true,
    "lib": ["es2017", "es7", "es6", "dom"],
    "module": "CommonJS",
    "target": "es5"
  },
  "files": [
    "test.ts"
  ]
}
Run Code Online (Sandbox Code Playgroud)

只是为了测试,我添加了test.ts. 其内容是

import Axios from "axios";

var axios = Axios.create();
axios.get("https://www.example.com");
Run Code Online (Sandbox Code Playgroud)

现在,当我运行构建过程时,这就是结果

"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var axios_1 = require("axios");
var axios = axios_1.default.create();
axios.get("https://www.example.com");
Run Code Online (Sandbox Code Playgroud)

当我在我的中使用它时index.html

<script src="test.js"></script>

它只是说ReferenceError: exports is not defined


我无法想象使用与浏览器兼容的 JavaScript 的导入来编译 TypeScript 会如此困难。任何帮助将不胜感激。

javascript typescript ecmascript-6

7
推荐指数
1
解决办法
752
查看次数