考虑Parameters实用程序类型,其底层类型是元组:https ://www.typescriptlang.org/docs/handbook/utility-types.html#parameterstype
我有一个功能SomeFunction。要将函数的参数用作类型,我编写Parameters<SomeFunction>.
现在假设我想使用函数的参数作为除第一个参数之外的类型。
显然对于数组我会使用类似的东西...args.slice(1)。但我不知道用于 Typescript 定义的切片实用程序。Omit仅适用于对象。
这个问题的答案提供了一个RemoveFirstFromTuple实用程序。但这有点复杂。是否有一种内置方法可以在类型定义中提取元组的一部分?
我有四个元素显示为按钮.两个是<button>s,一个是一个<a>,一个是<input type="submit">.
的文字<a>是非常轻微垂直相对偏移了别人,我想不通为什么.这种情况发生在Chrome,Firefox,Safari和IE 11中.
<!DOCTYPE html>
<html>
<head>
<title>Buttons</title>
<style>
*{
margin:0;
padding:0;
border:0;
border-collapse:collapse;
border-spacing:0;
font-size:inherit;
font-style:inherit;
font-family: "Helvetica Neue", Helvetica, sans-serif;
font-weight:inherit;
text-decoration:none;
color:inherit;
background-color:transparent;
list-style-type:none;
}
button,
.button,
input[type=submit]{
cursor:pointer;
padding:10px;
margin-bottom:10px;
box-sizing:border-box;
border-radius:5px;
background-color:#eee;
line-height:24px;
height:48px;
display:inline-block;
vertical-align:middle;
text-transform:uppercase;
letter-spacing:2px;
background-color:#69c;
color:#fff;
font-weight:bold;
text-align:center;
font-size:10px;
transition: background-color 0.1s, border-color 0.1s, color 0.1s;
}
button:hover,
.button:hover,
input[type=submit]:hover{
background-color:#7ad;
}
</style>
</head>
<body>
<button>Foo</button>
<button>Bar</button>
<a class="button">Baz</a>
<input …Run Code Online (Sandbox Code Playgroud)我正在开发一个基于浏览器的项目,需要兼容 IE11(叹气)。Webpack 令人窒息async/await。这是我的控制台的输出:
Based on your code and targets, added regenerator-runtime.
...
Module not found: Error: Can't resolve 'regenerator-runtime/runtime'
Run Code Online (Sandbox Code Playgroud)
我看过很多与我类似的问题,但没有运气。许多人建议使用@babel/polyfill我避免使用的方法,因为它已被弃用。
是什么导致了这个问题?我希望它可以通过手动导入来修复regenerator-runtime/runtime,但它的主要卖点之一似乎babel-env是不必手动导入polyfills,所以我认为我错过了一个步骤。谢谢你!
这是我正在尝试运行的内容,它将被导入到另一个文件中:
class Fetcher {
constructor() {
this.form = document.querySelector('form');
this.form.addEventListener('submit', this.onSubmit.bind(this));
}
async onSubmit(event) {
event.preventDefault();
const apiResponse = await fetch(`${WP_url}/api`);
const apiJson = await apiResponse.json();
console.log(apiJson);
}
}
export default Fetcher;
Run Code Online (Sandbox Code Playgroud)
webpack.config.js:
const path = require('path');
function pathTo(filepath) {
return path.join(__dirname, filepath);
}
module.exports = function(env, …Run Code Online (Sandbox Code Playgroud) 我的项目django-heroku在它的 Pipfile 中有一个package.
django-heroku具有gunicorn在其Pipfile作为dev-package。见:https : //github.com/heroku/django-heroku/blob/master/Pipfile
我希望pipenv install --dev在我的项目中运行后,我可以运行pipenv run gunicorn.
但它抛出以下错误:
Error: the command gunicorn could not be found within PATH or Pipfile's [scripts].
如果开发依赖项不可用,那有什么意义install --dev?
我只是对<input type="submit" />标签的安全性感到好奇.
如果我有一个表单(带method="post")只有一个"提交"按钮,它被禁用,并且我没有写任何影响表单,按钮或其他内容的JS/AJAX/jQuery,有人仍然可以找到一个提交表格的方式?
这是我正在谈论的表格的代码:
<form method="post" action="processor.php" enctype="multipart/form-data">
<input type="text" name="foo" value="bar" />
<input type="submit" value="Submit" disabled="disabled" />
</form>
Run Code Online (Sandbox Code Playgroud)
非常感谢!
编辑:Duh.当然它不安全,因为"禁用"完全在客户端,就像所有HTML一样.我是个白痴.
以下是我创建一个示例 Angular 应用程序所采取的步骤:
npm i -g @angular/cli (安装 Angular 11.2)ng new foocd foo.browserslistrc用last 1 Chrome version。(运行npx browserslist然后只打印chrome 89,正如预期的那样。)ng build --prod结果如下:
$ ng build --prod
? Browser application bundle generation complete.
? Copying assets complete.
? Index html generation complete.
Initial Chunk Files | Names | Size
main.feaf6a85348d901578d8.js | main | 211.52 kB
polyfills.00096ed7d93ed26ee6df.js | polyfills | 35.98 kB
runtime.7b63b9fd40098a2e8207.js | runtime | 1.45 …Run Code Online (Sandbox Code Playgroud) 我多年来一直在编写Javascript,但刚才意识到我并不完全理解这些"HTML-y"方法实际上是如何与DOM交互的.
我的假设是,当HTML被解释为DOM时,浏览器会保留关于哪个HTML元素成为哪个DOM节点的引用.
因此,.getElementById当您选择元素时,浏览器会通过引用返回相关节点.为什么不这样做.getNodeById而且更正确?
我的假设是.innerHTML浏览器在运行时将其解释为DOM.但是,在像IE7这样的旧浏览器中,.innerHTML不能用于创建表节点.这告诉我,它原本只是为了修改现有节点的文本属性,但它似乎很奇怪,它存在,我们不只是使用.innerText.
我想我只是对一些Javascript历史感到困惑.
考虑:我想将一段文本作为include标签中的参数传递。文本块包含引号。例如:
Breaking news:
Area man says, "This is newsworthy!"
Run Code Online (Sandbox Code Playgroud)
我可以想到几种方法来做到这一点:
{%
include newsitem.html
content='Breaking news:
Area man says, "This is newsworthy!"'
%}
Run Code Online (Sandbox Code Playgroud)
这并不理想,因为文本块中可能有单引号/撇号。
captures{% capture newscontent %}
Breaking news:
Area man says, "This is newsworthy!"
{% endcapture %}
{%
include newsitem.html
content=newscontent
%}
Run Code Online (Sandbox Code Playgroud)
这并不理想,因为它很冗长。
{%
include newsitem.html
content="Breaking news:
Area man says, \"This is newsworthy!\""
%}
Run Code Online (Sandbox Code Playgroud)
这并不理想,因为如果有很多引号,那么它们都需要反斜杠,添加起来会很麻烦。
captures和include{% capture content %}{% include_relative newscontent.md %}{% endcapture %}
{% …Run Code Online (Sandbox Code Playgroud) 我有一个Factory应该返回特定类型的通用函数:
type Factory<T> = () => T;
interface Widget {
creationTime: number;
}
const build: Factory<Widget> = () => {
return {
creationTime: Date.now(),
foo: 'bar',
};
};
Run Code Online (Sandbox Code Playgroud)
我希望 Typescript 抛出错误,因为 foo它不是接口 Widget 上的属性。然而,事实并非如此。
但是如果我将widgetFactory函数修改为下面的代码——唯一的区别是我明确声明了返回类型——那么它确实会抛出一个错误:
const build: Factory<Widget> = (): Widget => {
return {
creationTime: Date.now(),
foo: 'bar',
};
};
Run Code Online (Sandbox Code Playgroud)
有没有办法让 Typescript 为我的泛型Factory类型分配相同的“严格性” ?
我有一个类,并给它的原型几个子对象,以方便命名空间.这些子对象有方法.我无法弄清楚如何this在这些方法中使用来访问使用构造函数设置的属性.
我有一种感觉bind,call并且apply以某种方式参与其中,但是我很难理解那些做什么,以及所有这些OOP-ness的工作原理.有很多资源,但它们都是太低级别,或者高级别我不理解它们.谢谢!
function Object(
argument1,
argument2
){
this.property1 = argument1;
this.property2 = argument2;
}
Object.prototype = {
subObject1 : {
method1 : function(){
return this.property1;
}
},
subObject2 : {
method1 : function(){
return this.property2;
}
}
}
var foo = new Object(11, 22);
var bar = new Object(33, 44);
console.log(foo.subObject1.method1()); //I'd like this to return 11
console.log(foo.subObject2.method1()); //I'd like this to return 22
console.log(bar.subObject1.method1()); //I'd like this to return 33
console.log(bar.subObject2.method1()); …Run Code Online (Sandbox Code Playgroud)