Heim  >  Fragen und Antworten  >  Hauptteil

Typoskript: dynamische Liste aus einem Array von Tupeln, keine Schnittmenge

<p><pre class="brush:php;toolbar:false;">const intents = ["primär", "sekundär", "Akzent", "Gefahr"] as const; const buttonSizes = ["klein", "mittel", "groß"] as const; type IntentType = (typeof intents)[number]; type SizeType = (typeof buttonSizes)[number]; Typ ButtonProps = { Absicht?: IntentType; Größe?: SizeType; } & { [K in IntentType als `${Lowercase<K>}`]?: boolean; };</pre> <p>在这段代码中,我希望 Vue 组件能够接收如下属性 </p> <p>或者 喜欢 </p> <p>Wenn ich den Code jetzt statischer schreibe, wird Folgendes angezeigt:</p> <pre class="brush:php;toolbar:false;">type ButtonProps = { Absicht?: "primär" | "sekundär" | „Akzent“ | „Gefahr“; Größe?: "klein" | „mittel“ | "groß"; primär?: boolean; sekundär?: boolean; Akzent?: boolean; Gefahr?: boolean; }</pre> <p> <p>第一个示例有效,但由于某种原因 VUE 抛出错误</p> <blockquote> <p>内部服务器错误:[@vue/compiler-sfc] 类型参数传递给 DefineProps() ist eine neue Funktion für die Verwendung von DefineProps() 或文字类型.</p> </blockquote> <p>这个错误似乎是已知的并且正在被解决,所以看起来</p> <p>
P粉163951336P粉163951336414 Tage vor428

Antworte allen(1)Ich werde antworten

  • P粉384244473

    P粉3842444732023-09-01 18:26:56

    鉴于错误显示“引用接口或文字类型”,我假设将 ButtonProps 定义为扩展基本类型的接口应该有效:

    type IntentAndSize = {
      [K in IntentType as `${Lowercase<K>}`]?: boolean;
    };
    
    interface ButtonProps extends IntentAndSize {
      intent?: IntentType;
      size?: SizeType;
    }

    游乐场

    Antwort
    0
  • StornierenAntwort