有人可以提供一个简单的例子来演示实现装饰器的正确方法,并描述可能的有效装饰器签名中的参数是什么意思吗?
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)
另外,在实现装饰器时是否应该记住哪些最佳实践注意事项?
如何获取数组中的唯一值列表?我是否总是必须使用第二个数组,或者在JavaScript中是否有与java的hashmap类似的东西?
我将只使用JavaScript和jQuery.不能使用其他库.
我使用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方法?
两个功能的故事
我有一个函数填充数组到指定的值:
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) 有没有办法让我的代码用ts-node编译,即使我的.ts
文件的一行没有未使用的属性警告而没有"noUsedLocals": false
在我的tsconfig.json
文件中设置?
如何将Timeout属性设置为Windows.Web.Http.HttpClient
operation.我使用的代码示例如下.
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
我正在我的应用程序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 代码保存到文件中。这对我的应用程序来说将是一个不必要的性能负担。我只想编译和键入保存在内存中的代码片段。
如何将这些类序列化为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) 我的项目中有一个.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 会如此困难。任何帮助将不胜感激。
typescript ×6
javascript ×5
ecmascript-6 ×2
c# ×1
datetime ×1
decorator ×1
generator ×1
httpclient ×1
jquery ×1
momentjs ×1
transpiler ×1
tslint ×1
typechecking ×1
windows-8.1 ×1