简体   繁体   English

如何从 typescript 中的动态数组获取动态类型?

[英]How do I get a dynamic type from a dynamic array in typescript?

I've searched for a solution already and found const assertions but Typescript gives me a type error that I can only use const assertions on certain types... Probably referring to that the array I want to assert is not set in the same file but rather will be set by the user who will be using my module.我已经搜索了一个解决方案并找到了 const 断言但是 Typescript 给了我一个类型错误,我只能在某些类型上使用 const 断言......可能是指我想要断言的数组没有设置在同一个文件中但是而是将由将使用我的模块的用户设置。 To explain what I mean I have some code below.为了解释我的意思,我在下面有一些代码。

The following code block is the file HighLight.ts for example.以下代码块以文件 HighLight.ts 为例。

type Languages = "javascript" | "typescript" | "json" | "java" | "kotlin" | "python";

export default class HighLight {
  private languages: Languages | Languages[];

  constructor({ languages }: { languages: Languages | Languages[] }) {
    this.languages = <const>languages;
  }
}

And I import it in the index.ts file然后我将它导入到 index.ts 文件中

import HighLight from "HighLight.ts";

new HighLight(["javascript", "typescript"])

To give some more context, I want to create a module that can highlight code using highlight.js and have it as a pure string which you can print to the console, essentially a port of highlight.js for nodejs but purely for console applications.为了提供更多上下文,我想创建一个模块,它可以使用 highlight.js 突出显示代码,并将其作为纯字符串打印到控制台,本质上是 highlight.js 的端口,用于 nodejs 但纯粹用于控制台应用程序。

Because I want my implementation to import all languages only as needed (like highlight.js) the user has to provide a list of languages they plan on highlighting later on.因为我希望我的实现仅在需要时导入所有语言(如 highlight.js),所以用户必须提供他们计划稍后突出显示的语言列表。 I've figured out the importing part already but I haven't attached that code as I think it is irrelevant to this problem.我已经弄清楚了导入部分,但我没有附加该代码,因为我认为它与这个问题无关。

With that out of the way, I wanted to create a highlight method which takes in the code and the language.顺便说一句,我想创建一个包含代码和语言的突出显示方法。 It would be nice if languages is restricted to only the languages you've given the constructor when creating an instance.如果语言仅限于您在创建实例时为构造函数提供的语言,那就太好了。 What I thought to be an easy task with a const assertion turned out to be hard.我认为使用 const 断言是一项简单的任务,结果却很难。 A const assertion in this scenario doesn't work as the array/string is unknown at the moment but later set by the user when calling the constructor... I also noticed that if the array is statically typed but in a different file a const assertion also does not work sadly.在这种情况下,const 断言不起作用,因为此时数组/字符串未知,但稍后由用户在调用构造函数时设置...我还注意到,如果数组是静态类型的,但在不同的文件中是 const可悲的是断言也不起作用。

Is there a different way of getting that type for the highlight method?是否有不同的方法来为突出显示方法获取该类型?

type Languages = "javascript" | "typescript" | "json" | "java" | "kotlin" | "python";

export default class HighLight<L extends Languages> {
  private languages: L | L[];

  constructor({ languages }: { languages: L | L[] }) {
    this.languages = languages;
  }
}

let l = new HighLight({languages: 'javascript'})
//  ^? let l: HighLight<"javascript">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM