私が使用しているLIB(アフロディーテ)は、このタイプの定義があります。Typescriptでインデックスシグネチャを絞り込むことはできますか?
interface StyleDeclaration {
[key: string]: CSSProperties;
}
私はkey
sが指定されたリストのサブセットである必要があり、サブタイプを作成したいのですが、サブタイプとして使用することができますStyleDeclaration
。
私の最高の試みがされています:これは、ポイントまで、うまく機能
type CssTransStyleName =
'defaultStyle' | 'activeStyle' | 'etcetera'
type CssTransStyleDeclaration = {[K in CssTransStyleName]?: CSSProperties}
- 私はStyleDeclarationとしてインスタンスを使用しようとするポイント。例えば、与えられた:
Type 'CssTransStyleDeclaration' is not assignable
to parameter of type 'StyleDeclaration'.
Property 'defaultStyle' is incompatible with index signature.
Type 'CSSProperties | undefined' is not assignable to type 'CSSProperties'.
Type 'undefined' is not assignable to type 'CSSProperties'.
どうやら、コンパイラはCssTransStyleName
内のすべてのプロパティは、型の値を持っている」という意味として
{[K in CssTransStyleName]?: CSSProperties}
を解釈:
const cssDeclaration: CssTransStyleDeclaration = {
style: {
transformOrigin: 'top left',
},
}
const ss: StyleDeclaration = cssDeclaration
コンパイラは、と文句を言いCSSProperties | undefined
、私はいくつかのプロパティが存在し、いくつかは存在しないと言うことを試みたのに対し、
キャスティングas StyleDeclaration
が動作しますが、ダクトテープを使用しないことをおすすめします。
私も
type CssTransStyleDeclaration = {[K in CssTransStyleName]?: CSSProperties} & StyleDeclaration
を試してみましたが、これは単にStyleDeclaration
として、それが効果的に同じになります。プロパティはCssTransStyleName
のプロパティに限定されません。
エラーを生成するコードを投稿できますか? –
@shambalambalaありがとう、更新済み - 上記を参照してください –