将JS转换为打字稿 [英] Convert JS to Typescript

查看:77
本文介绍了将JS转换为打字稿的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Javascript应用,该应用可对数组内的重复数进行计数...

I have a simple Javascript app that counts duplicate numbers inside of an array...

我目前正在学习Typescript,我想将此Javascript转换为Typescript ...

I am currently learning Typescript and I would like to turn this Javascript into a Typescript...

我已经在机器上启动并运行了angular,但是同时学习有关Angular 2.0和Typescript的所有知识时,它可能会变得不知所措.

I have angular up and running on my machine, but it can get overwhelming when learning everything about Angular 2.0 and Typescript at the same time.

将其转换为Typescript的最佳方法是什么?

What would be the best way to convert this to a Typescript?

$(document).ready(function() {
  var n = prompt("Enter your numbers").split(",");
  console.log(n);

  var counts = {};
  n.forEach(function(x) {
    counts[x] = (counts[x] || 0) + 1;
  });

  document.write(
    "<h1>Enter numbers when prompted!</h1>" +
      "<p>You entered following numbers:</p>" +
      n +
      "<br/>" +
      "<p>The occurence is as follows:</p>" +
      JSON.stringify(counts)
  );
});

推荐答案

您真的不需要执行任何操作,您的JS在TS中也可以正常工作.但是,如果您想利用所有功能,它的外观将是这样:

You really don't have to do anything, your JS would work fine in TS as well. But if you want to take advantage of all features this is how it would look:

$(document).ready(() => {

    var n = prompt("Enter your numbers").split(",");
    console.log(n);

    var counts : { [nr: string]: number } = {};
    n.forEach((x) => { 
        counts[x] = (counts[x] || 0) +1; 
    });

    document.write(`
    <h1>Enter numbers when prompted!</h1>
    <p>You entered following numbers:</p>${n}<br/>
    <p>The occurence is as follows:</p>${JSON.stringify(counts)}`);

});

这篇关于将JS转换为打字稿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆