小编art*_*tem的帖子

打字稿:String和string之间的区别

有没有人知道TypeScript中String和string之间的区别?假设它们应该是相同的,我是否正确?

var a: String = "test";
var b: string = "another test";
a = b;
b = a; // this gives a compiler error!
Run Code Online (Sandbox Code Playgroud)

当前版本的编译器说:

Type 'String' is not assignable to type 'string'.
  'string' is a primitive, but 'String' is a wrapper object.
     Prefer using 'string' when possible.
Run Code Online (Sandbox Code Playgroud)

那是一个错误吗?

typescript

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

为什么我在TypeScript中得到"Type'String'不能用作索引类型"错误

我有大量的js代码,它们使用了许多辅助数组,可以非常快速地访问对象.所以我尝试移植此代码并失败.很明显我还不太了解TS.

我已经找到了这个问题的答案,并找到了创建界面等建议...但这似乎有点偏离轨道.

所以看似明显的尝试:

 class Foo {
   allObjects: MyObject[];
    constructor() {
     this.allObjects = [];
   }
}
Run Code Online (Sandbox Code Playgroud)

MyObject的地方 - 简化:

 class MyObject {
     _id: String;
     public getId(): String {
        return this._id;
     }
 }
Run Code Online (Sandbox Code Playgroud)

我相信:

 myObjectInstance: MyObject;
 fooInstance.allObjects[myObjectInstance.getId()] = myObjectInstance;
Run Code Online (Sandbox Code Playgroud)

会没问题...我看到可爱的TS2342错误(索引表达式参数必须是'string','number'或'any'类型)这对我没有意义,因为getId()是'string' .这似乎不应该那么难.所以你的帮助将不胜感激......

typescript

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

SolidJS:For 与 Index

在他们的渲染列表教程中,他们说:

<Index>组件是为这些情况提供的。根据经验,在使用基元时使用<Index>.

<For>关心数组中的每条数据,并且该数据的位置可以改变;<Index>关心数组中的每个索引,并且每个索引处的内容都可以更改。

这两句话对我来说都没有意义。“使用基元时”是什么意思?我总是使用数组。有人可以澄清何时使用ForvsIndex吗?

solid-js

24
推荐指数
2
解决办法
5514
查看次数

为 ES 模块做 require.resolve

我需要解析通过静态导入或类似函数的动态导入导入的ES 模块,其方式类似于node.js使用require.resolve()解析CJS 模块的方式。ES模块是否存在类似的东西?

例如,如果一个 Vue 包同时具有vue.runtime.common.jsvue.runtime.esm.js. 我需要获得vue.runtime.esm.js. 如果包裹没有,我想知道。

javascript es6-modules

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

system.js es6模块无法启动

根据入门我创建了一个名为main.js的文件:

 export class q {
    constructor() {
      this.es6 = 'yay';
    }
  }
Run Code Online (Sandbox Code Playgroud)

在我的html文件中得到:

<script src="system.js"></script>
<script>
  SystemJS.import('main.js')
  .then(null,(err)=>console.error(err))
</script>
Run Code Online (Sandbox Code Playgroud)

加载页面时,我得到:

错误:无法动态转换ES模块需要通过配置加载程序插件SystemJS.config({ transpiler: 'transpiler-module' }).实例化http:// localhost:8080/main.js 在system.js上的transpile(system.js:3650)加载main.js:3433 SystemJS.import.then @ index.html:17

我从这里得到的system.js文件:https://raw.githubusercontent.com/systemjs/systemjs/master/dist/system.src.js

使用Chrome中安装的缓存杀手插件从http-server(节点)运行静态内容.

不知道这是什么,但我会继续阅读文档,也许找到解决方案.虽然有点邋in它在"开始"时打破.

更新

我猜这与它有关:

https://github.com/jspm/jspm-cli/issues/1312

但是没有任何关于如何使错误消失的信息.代码在Chrome中运行,并且将由babel进行转换,只有部分我需要system.js用于开发期间加载模块.

更新

https://youtu.be/5P04OK6KlXA?t=3m3s

看起来像"入门"应该提到,为了工作的例子,也需要跟踪.

经过一番搜索,我在这里找到了traceur.js:http://google.github.io/traceur-compiler/bin/traceur.js

将html更改为:

<script src="traceur.js"></script>
<script src="system.js"></script>
<script>
  SystemJS.import('./main.js')
  .then(null,(err)=>console.error(err))
</script>
Run Code Online (Sandbox Code Playgroud)

它仍然是同样的错误.

更新

使用npm安装systemjs和traceur并将它们添加到html:

<script src="./node_modules/traceur/bin/traceur.js"></script>
<script src="./node_modules/systemjs/dist/system.js"></script>
Run Code Online (Sandbox Code Playgroud)

同样的错误.我已经完成了这个并恢复到requirejs用于开发,r.js用uglify进行分发.

在阅读了有关汇总的内容以及编译后的代码如何只导入您需要的内容而不是整个文件时,我们正在研究这个问题.

javascript systemjs es6-modules

6
推荐指数
0
解决办法
1294
查看次数

TypeScript类型推断/缩小挑战

我目前正在尝试改进现有代码的类型.我的代码看起来大致如下:

/* dispatcher.ts */
interface Message {
    messageType: string;
}

class Dispatcher<M extends Message> {    
    on<
        MessageType extends M["messageType"],
        SubMessage extends M & { messageType: MessageType }
    >(
        messageType: MessageType,
        handler: (message: SubMessage) => void
    ): void { }
}

/* messages.ts */
interface AddCommentMessage {
    messageType: "ADD_COMMENT";
    commentId: number;
    comment: string;
    userId: number;
}

interface PostPictureMessage {
    messageType: "POST_PICTURE";
    pictureId: number;
    userId: number;
}

type AppMessage = AddCommentMessage | PostPictureMessage;

/* app.ts */
const dispatcher = new Dispatcher<AppMessage>();

dispatcher.on("ADD_COMMENT", …
Run Code Online (Sandbox Code Playgroud)

inference type-inference narrowing typescript

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

使用严格的空检查处理Typescript 2.0中的数组移位返回类型

在我的具有严格空检查的Typescript 2.0项目中,我有一个数组:

private _timers: ITimer[]

和if语句:

if(this._timers.length > 0){
  this._timers.shift().stop();
}
Run Code Online (Sandbox Code Playgroud)

但我得到一个编译错误:

Object is possibly 'undefined'

我怎样才能说服编译器它没有未定义?

我可以像这样绕过它:

const timer = this._timers.shift();
if(timer){
  timer.stop();
}
Run Code Online (Sandbox Code Playgroud)

但这似乎有点过于冗长,并且不必要地使用变量来绕过打字约束.

谢谢

typescript typescript2.0

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

SolidJS:对于子组件不渲染列表

我有一个父组件返回:

<List list={list()}>
    {(item, index) => <div>{item}</div>}
</List>
Run Code Online (Sandbox Code Playgroud)

其中list是创建的信号。List是我返回的自定义组件:

<div>
    <For each={list}>{children}</For>
</div>
Run Code Online (Sandbox Code Playgroud)

但每当list更新时,它都不会呈现。当我将For代码移至父组件时,它会进行渲染,那么将信号的值传递给子组件以使其在更新时不会重新渲染又是什么呢?

编辑:演示

import { render } from "solid-js/web";
import { createSignal, For } from "solid-js";

function Counter() {
  const [list, setList] = createSignal([]);
  const increment = () => setList([...list(), 1]);

  return (
    <>
      <button type="button" onClick={increment}>
        add
      </button>
      <p>broken list</p>
      <List list={list()} />

      <p>other list</p>
      <For each={list()}>
        {(item) => <p>{item}</p>}
      </For>
    </>
  );
}

function List({list}) {
  return …
Run Code Online (Sandbox Code Playgroud)

solid-js

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

在 HTML 文件中使用 System.js 调用 typescript 类方法

我正在尝试 Typescript 和 System.js。Typescript 编译器使用 -w 标志运行。

我想在页面加载时调用一个方法,我可以导入播放,但不能使用“then”访问它:

HTML文件:

<!doctype html>
<head>
  <script src='//cdnjs.cloudflare.com/ajax/libs/systemjs/0.19.41/system.js'> </script>
  <script>
    System.import('./play.js').then(function(play) { 
      play.start(); 
    });
  }
  </script>
</head>
<body>
</body>
Run Code Online (Sandbox Code Playgroud)

play.ts 文件:

export class Play {
  start() {
    alert('hello...');
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到错误:Uncaught (in promise) TypeError: play.start is not a function。有任何想法吗?

typescript systemjs

3
推荐指数
1
解决办法
818
查看次数

UML diagram for namespace

Someone please tell me how to draw a UML diagram for namespace.
My code has class uses functionality from a namespace and also namespace that uses functions from other namespace.
I cannot find a proper way to draw its UML diagram. Please guide a bit.
Thanks in advance

c++ uml namespaces

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