小编zzr*_*zrv的帖子

打字稿:esnext 编译器选项会破坏来自外部库的 es6 导入

当将编译器选项的以太moduletarget属性设置为 esnext(id 喜欢使用import("example")语句)时,es6 导入语句将停止为npm installed 库工作(本地模块仍然工作:例如"./test.ts")。

所以这import * as asd from "testmodule";抛出cannot find module 'testmodule'. 然而,省略这两个属性使它工作。这是为什么,我应该使用什么标准来保留import("example")import * as asd from "testmodule";声明?


这是我的完整 tsconfig.json:

{
  "compilerOptions": {
    "outDir": "./dist/",
    "module": "esnext",
    "target": "esnext",
    "allowJs": true,
    "sourceMap": true
  }
}

Run Code Online (Sandbox Code Playgroud)

node-modules typescript

13
推荐指数
1
解决办法
1万
查看次数

在nodejs中未定义array.prototype.flat

给出以下js命令:

[].flat;
Run Code Online (Sandbox Code Playgroud)

在浏览器(chrome / firefox)中执行:返回 function

使用nodejs v10.13.0执行它:返回 undefined

现在,我想知道节点RTE中还没有其他什么方法,像全局对象这样的文档在哪里Array

似乎与相同Array.prototype.flatMap

javascript arrays node.js

11
推荐指数
2
解决办法
3906
查看次数

Java Generics很奇怪

我正在使用java 8.

我最近遇到过这个:

public class Test {
    public static void main(String[] args) {
        String ss = "" + (Test.<Integer>abc(2));
        System.out.println(Test.<Integer>abc(2));
    }
    public static <T> T abc(T a) {
        String s = "adsa";
        return (T) s;
    }
}
Run Code Online (Sandbox Code Playgroud)

这不会抛出java.lang.ClassCastException.这是为什么?

我一直在思考+System.out.println打电话toString.但是当我尝试这样做时,它会按预期抛出异常.

String sss = (Test.<Integer>abc(2)).toString();
Run Code Online (Sandbox Code Playgroud)

java generics casting

11
推荐指数
2
解决办法
822
查看次数

browserify + tsify + babelify; 巴贝尔被忽视了

我想浏览,tsify和babelify我的代码.Browserify和其他一个开发者工作,但他们在一起.Babel似乎被忽略了(甚至没有读过.babelrc).

我有以下gulp代码:

const gulp = require("gulp");
const browserify = require("browserify");
const source = require('vinyl-source-stream');
const tsify = require("tsify");
const babelify = require("babelify");

function build() {

  var b = browserify({
    basedir: '.',
    debug: true,
    cache: {},
    entries: ['src/index.ts'],
    packageCache: {}
  });

  return b
    .plugin(tsify)
    .transform(babelify)
    .bundle()
    .on("error", function (err) { console.log("Error: " + err.message); })
    .pipe(source('build.js'))
    .pipe(gulp.dest("build"));
 }

 gulp.task("build", build);
Run Code Online (Sandbox Code Playgroud)

有了这个babelrc

{
   "presets": ["minify"]
}
Run Code Online (Sandbox Code Playgroud)

和那些依赖

"@babel/core": "^7.2.2",
"babel-preset-minify": "^0.5.0",
"babelify": "^10.0.0",
"browserify": "^16.2.3",
"gulp": "^4.0.0",
"tsify": "^4.0.1",
"typescript": "^3.2.2",
"vinyl-source-stream": …
Run Code Online (Sandbox Code Playgroud)

javascript browserify typescript gulp babeljs

10
推荐指数
1
解决办法
438
查看次数

slub动画的flubber或animejs

使用flubber / animejs修改svg路径的优缺点是什么?

animejs的相比,flubber的体积似乎更大(就包大小而言)。flubber用所有这些代码做什么?它的性能更高吗?

javascript animation svg

5
推荐指数
0
解决办法
43
查看次数

Typescript: Instance of class type

I have the type of a class like here as A_Type?

class A {
  constructor(public a: string) {}
}

type A_Type = {new (a: string): A}
Run Code Online (Sandbox Code Playgroud)

And Id like to get the type of the instance of the A_Type constructor, so that I could type this function explicitly

class A {
  constructor(public a: number) {}
}
//                                              ** not working **
function f<W extends { new(a: number): A }>(e: W): instanceof W {
  return new e(2)
}

let w …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
1
解决办法
1万
查看次数

Webcomponents:host:hover

有没有办法在webcompoenents上添加css:hover效果(不是来自父级)。

我有以下示例代码

window.customElements.define('a-b', class extends HTMLElement {
  constructor() {
      super();
      this.attachShadow({mode: 'open'}).innerHTML = `
        <style>
          :host {
            display: block;
            height: 100px;
            width: 100px;
            background: red;
          }
          :host:hover {
            background: blue;
          }
          :host::selection {
            background: rgba(0, 0, 0, 0.15);
          }
        </style>
        <slot></slot>
      `;
  }
}
Run Code Online (Sandbox Code Playgroud)

:host::selection物业按预期工作。但是:host:hover似乎没有任何作用。

我知道有解决此问题的方法,例如:

  • 包裹一切 div
  • :hover在父样式表上应用效果

但是我想知道我是否只是为了简化代码而错过了一些东西(因为这似乎不太复杂)。

html javascript css web-component shadow-dom

4
推荐指数
1
解决办法
405
查看次数

JS:异步函数隐式解开 Promise?

据我所知,异步函数将其返回值隐式包装到承诺中。这确实适用于所有属性,除了 Promise 本身。

async function f() {
  return new Promise((res) => {
    setTimeout(() => {
      res("Why am I being unwrapped")
    }, 1000)
  })
}

(async () => {
  console.log(await f())
})()
Run Code Online (Sandbox Code Playgroud)

那些在退回之前会被拆开。所以这await f()实际上等待着两个嵌套的承诺。

请注意,这也适用于显式创建的 Promises ( Promise.resolve(new Promise(...)))

有没有好的办法避免这种情况呢?我真的很想有一个嵌套的 Promise,而不需要像这样的快速修复。

async function y() {
  return {wrapped: new Promise((res) => {
    setTimeout(() => {
      res("Why am I being unwrapped")
    }, 1000)
  })}
}

(async () => {
  console.log((await y()).wrapped)
})()
Run Code Online (Sandbox Code Playgroud)

沙盒

javascript asynchronous node.js promise async-await

2
推荐指数
1
解决办法
2643
查看次数