TypeScript, - strictNullChecks模式.
假设我有一个可以为空的字符串数组(字符串| null)[].以表达式为string []的方式删除所有空值的单表达式方法是什么?
const array: (string | null)[] = ["foo", "bar", null, "zoo", null];
const filterdArray: string[] = ???;
Run Code Online (Sandbox Code Playgroud)
Array.filter在这里不起作用:
// Type '(string | null)[]' is not assignable to type 'string[]'
array.filter(x => x != null);
Run Code Online (Sandbox Code Playgroud)
数组理解可能有效,但TypeScript不支持它们.
实际上,通过从联合中删除具有一个特定类型的条目,可以将该问题推广到过滤任何联合类型的数组的问题.但是让我们关注具有null和未定义的联合,因为这些是最常见的用例.
最近我开始重构我正在使用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);
}
Run Code Online (Sandbox Code Playgroud)
现在我正在寻找解决方案来处理指令定义的类似情况.我找到了一个很好的做法,将指令定义为函数:
module directives {
export function myDirective(toaster): ng.IDirective {
return {
restrict: 'A',
require: ['ngModel'],
templateUrl: 'myDirective.html',
replace: true,
link: (scope: ng.IScope, element: ng.IAugmentedJQuery, attrs: ng.IAttributes, ctrls) =>
//use of $location service
...
}
};
}
angular.module('directives', [])
.directive('myDirective', ['toaster', myDirective]);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我被迫在指令定义中定义Angular依赖项,如果定义和TypeScript类在不同的文件中,则可能非常容易出错.使用typescript和$inject机制定义指令的最佳方法是什么,我正在寻找一种实现TypeScript IDirectiveFactory接口的好方法,但我对我找到的解决方案并不满意.
快问,我想在启动没有返回值的异步任务之前等待一秒钟.
这是正确的方法吗?
Task.Delay(1000)
.ContinueWith(t => _mq.Send(message))
.Start();
Run Code Online (Sandbox Code Playgroud)
异常会发生什么?
我正在用webpack构建两个项目; 一个是另一个的图书馆.
在构建我的包装器项目时,是否可以从我的库项目中使用源图?我希望能够从我的包装器UI调试我的库代码.
我的构建工作正常,因为库是内置的.唯一的问题是源图.我在浏览器调试器中看到的JavaScript是丑化的,因为源映射不可用.
我的项目结构的片段:
+-- my-ui/
+-- dist/
+-- my-ui.js
+-- my-ui.js.map
+-- node_modules/
+-- my-lib/
+-- dist/
+-- bundle.js
+-- bundle.js.map
Run Code Online (Sandbox Code Playgroud)
片段来自webpack.config.js:
module.exports = {
entry: './src/js/main.jsx',
output: {
path: path.join(__dirname, 'dist'),
filename: 'my-ui.js',
library: 'my-ui',
libraryTarget: 'umd'
},
devtool: 'source-map',
module: {
loaders: [
{test: /\.jsx?$/, loader: 'babel', include: path.join(__dirname, 'src')}
]
},
plugins: [
new Clean('dist'),
new HtmlWebpackPlugin({
template: 'src/index.html',
inject: true
})
]
};
Run Code Online (Sandbox Code Playgroud) 我正在尝试将我的web api 2项目移动到ASP.NET 5.但是我有许多不再存在的元素.
例如IHttpActionResult或Ok(), NotFound()方法.或者RoutePrefix[]
我应该改变每一个IHttpActionResult用IActionResult?改变Ok()用new ObjectResult?(它是一样的吗?)
那怎么样HttpConfiguration似乎在startup.cs中不存在?
我在postgresql 9.4数据库中定义了一个数组字段:
character varying(64)[]
Run Code Online (Sandbox Code Playgroud)
我可以为该字段的默认值设置一个空数组,例如{}吗?设置的语法是什么?
如果只设置括号{},我会收到以下错误:
SQL error:
ERROR: syntax error at or near "{"
LINE 1: ...public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}
^
In statement:
ALTER TABLE "public"."accounts" ALTER COLUMN "pwd_history" SET DEFAULT {}
Run Code Online (Sandbox Code Playgroud) 我想知道是否可以添加默认控件选项我的位置按钮.

有没有办法让它成为默认值,或者我需要创建具有地理位置的按钮然后触发该按钮上的click事件以便将用户导航到当前位置?
我有这个界面:
export interface UserSettings
{
one: {
three: number;
four: number;
};
two: {
five: number;
six: number;
};
}
Run Code Online (Sandbox Code Playgroud)
...并想把它变成这个:
export interface UserSettingsForUpdate
{
one?: {
three?: number;
four?: number;
};
two?: {
five?: number;
six?: number;
};
}
Run Code Online (Sandbox Code Playgroud)
......但Partial<UserSettings>产生了这个:
{
one?: {
three: number;
four: number;
};
two?: {
five: number;
six: number;
};
}
Run Code Online (Sandbox Code Playgroud)
是否可以使用映射类型使所有深度上的所有属性都可选,或者我是否必须手动创建接口?
我目前正在尝试使用spawn.我试图从shell运行的是以下内容;
NODE_ENV=production node app/app.js
这是运行它的代码;
var spawn = require('child_process').spawn;
var start = spawn('NODE_ENV=production',['node','app/app.js']);
Run Code Online (Sandbox Code Playgroud)
但是,我收到了以下错误;
events.js:72
throw er; // Unhandled 'error' event
^
Error: spawn ENOENT
at errnoException (child_process.js:980:11)
at Process.ChildProcess._handle.onexit (child_process.js:771:34)
Run Code Online (Sandbox Code Playgroud)
我该怎么做spawn呢?
如何在另一个CSS calc函数中使用CSS calc函数?根据这篇文章,它是可能的,但没有这方面的例子.
typescript ×3
javascript ×2
.net ×1
angularjs ×1
asp.net-core ×1
asynchronous ×1
c# ×1
css ×1
css-calc ×1
css3 ×1
directive ×1
google-maps ×1
inject ×1
node.js ×1
owin ×1
postgresql ×1
reactjs ×1
sql ×1
webpack ×1