2017-12-05 8 views
0

問題は、このコード行から来ている。は取得「はエラー.find()メソッドの使用

const t: GridType = gridDef.find(a => { a.GridName == core.GridStyle; return a; }); 

私は取得していますエラー「ブール値に対するボイド割り当てられないが、この

ERRORであります(このvoid:a:GridType)=> voidの引数は の型には割り当てられません。ype 'src/app/grid-builder/builder-scratch.ts(255,43):エラーTS2345: (void:void、value:GridType、index:number、obj:GridType [])=>ブール値 ' ' void '型は' bo '型に代入できませんオレアン '。

これまでのように、グリッドを動的に作成するために、角型コンポーネントに拡張するクラスを定義しています。私たちは、私がしようとしているものをconstructor

constructor() { 
    this.Settings.GridStyle = 'SiteGridA'; 
    this.GridData = GridDefs; 
    this.buildGrid(this.Settings, this.GridData); 
} 

に戻って焦点を当てている場合、この

export interface GridType { 
    GridName : string; 
    Columns : GridLine[]; 
    Rows  : GridLine[]; 
} 

//other interfaces chaining into it 
export interface GridLine { 
    Names : LineName[]; 
    Type  : string; 
    CalcType : string; 
    CSize : CellSize; 
} 

export interface LineName { Name: string; } 

export interface CellSize { 
    Size? : number; 
    Repeat? : number; 
    Min? : number; 
    Max? : number; 
} 

のようなルックスと呼ばれている

export class GridBuilder{ 
    Settings : GridInit; 
    GridData : GridType[]; 
    Grid  : string = ''; 


    constructor() { 
     this.Settings.GridStyle = 'SiteGridA'; 
     this.GridData = GridDefs; 
     this.buildGrid(this.Settings, this.GridData); 
    } 

    buildGrid(core: GridInit, gridData: GridType[]) { 
     const w: number  = multiply(core.Size.Width, core.Size.PixelRatio); 
     const h: number  = multiply(core.Size.Height, core.Size.PixelRatio); 
     const o: string  = checkOrientation(w, h); 
     const c: CellSpecs = calcCell(o, w); 
     const t: GridType = gridData.find(a => { a.GridName == core.GridStyle; return a; }); 

     const cols: string = calcArea(t.Columns, c); 
     const rows: string = calcArea(t.Rows, c); 

     this.Grid = cols + '/' + rows; 
    } 
} 

GridType[]

  1. 設定されている達成しますパラメータ012グリッドに関するデータを取得するために、.find()メソッドで一致させる必要があります。
  2. .find()メソッドが検索する変数にグリッドデータを保存します。
  3. 変数を関数に渡して、グリッドを作成します。

GridDefsはかなりこの

export const GridDefs: GridType[] = [ 
    { 
     GridName : 'SiteGridA', 
     Columns : [ 
      { 
       Names : [ { Name: 'left-bleed-start' } ], 
       Type  : 'normal', 
       CalcType : 'divide', 
       CSize : { Size: 4 } 
      }, 
      { 
       Names : [ { Name: 'left-bleed-end'}, { Name: 'content-col-start' } ], 
       Type  : 'normal', 
       CalcType : 'multiply', 
       CSize : { Size: 32.5 } 
      }, 
      { 
       Names : [ { Name: 'content-col-end' }, { Name: 'right-bleed-start' } ], 
       Type  : 'normal', 
       CalcType : 'divide', 
       CSize : { Size: 4 } 
      }, 
      { 
       Names : [ { Name: 'right-bleed-end' } ], 
       Type  : 'end', 
       CalcType : 'none', 
       CSize : { Size: null } 
      } 
     ], 
     Rows  : [ 
      { 
       Names : [ { Name: 'top-bleed-start' } ], 
       Type  : 'normal', 
       CalcType : 'divide', 
       CSize : { Size: 4 } 
      }, 
      { 
       Names : [ { Name: 'top-bleed-end' }, { Name: 'link-row-start' } ], 
       Type  : 'normal', 
       CalcType : 'multiply', 
       CSize : { Size: 2 } 
      }, 
      { 
       Names : [ {Name: 'link-row-end'}, { Name: 'content-row-start' } ], 
       Type  : 'normal', 
       CalcType : 'none', 
       CSize : { Size: 0 } 
      }, 
      { 
       Names : [ { Name: 'content-row-end' }, { Name: 'footer-row-start' } ], 
       Type  : 'normal', 
       CalcType : 'multiply', 
       CSize : { Size: 4} 
      }, 
      { 
       Names : [ { Name: 'footer-row-end' }, { Name: 'bottom-bleed-start' } ], 
       Type  : 'normal', 
       CalcType : 'divide', 
       CSize : { Size: 4 } 
      }, 
      { 
       Names : [ { Name: 'bottom-bleed-end' } ], 
       Type  : 'end', 
       CalcType : 'none', 
       CSize : { Size: null } 
      } 
     ] 
    }, 
    { 
     GridName : 'LinkContainerA', 
     Columns : [ 
      { 
       Names : [ { Name: 'main-link-col-start' } ], 
       Type  : 'normal', 
       CalcType : 'none', 
       CSize : { Size: 0 } 
      }, 
      { 
       Names : [ { Name: 'main-link-col-end' }, { Name: 'feature-link-start' } ], 
       Type  : 'normal', 
       CalcType : 'percent', 
       CSize : { Size: 50 } 
      }, 
      { 
       Names : [ { Name: 'feature-link-end' } ], 
       Type  : 'end', 
       CalcType : 'none', 
       CSize : { Size: null } 
      } 

     ], 
     Rows  : [ 
      { 
       Names : [ { Name: 'content-row-sart' } ], 
       Type  : 'normal', 
       CalcType : 'none', 
       CSize : { Size: 0 } 
      }, 
      { 
       Names : [ { Name: 'content-row-end' } ], 
       Type  : 'end', 
       CalcType : 'none', 
       CSize : { Size: null } 
      } 
     ] 
    } 
] 

GridNameプロパティのように見える私はの線の内側にSettings変数でGridStyleパラメータと一致するようにしようとしています何であるグリッドの定義でありますエラーの原因となるコード

これまでのところ、OnInitを追加して、コンストラクタが起動する前にグリッドデータをロードしているかどうか確認してみました。私は1と3を試みた=符号をconstletのために切り替えてみました。私は本当にこれを超えて何をすべきかわかりません。私は同様の例で.find()メソッドを使用しましたが、問題はありませんでした。これをどうすれば解決できますか?

+0

検索:このように、

const t: GridType = gridData.find(a => a.GridName == core.GridStyle); 

または中括弧、正しい返し(および正しく入力)return文で値を:あなたはこのように、=>後にカッコを囲むことなく、簡単な表現であることを行うことができますnull(要素が一致する場合)を返すことができます。だからあなたは "共用体の型"を使うことができます。これは "const t:GridType | null = ..."と書いてあります。 – Eliseo

答えて

1

すべての足りない部分を充填した後、私はあなたの質問の1とは異なるエラーを取得しています:

const t: GridType = gridData.find(a => { a.GridName == core.GridStyle; return a; }); 

Argument of type '(this: void, a: GridType) => GridType' 
    is not assignable to parameter of type 
    '(value: GridType, index: number, obj: GridType[]) => boolean'. 
      Type 'GridType' is not assignable to type 'boolean'. 

今ではType 'GridType' is not assignable to type 'boolean'を言います。 GridTypeを持っているあなたはfind

{ a.GridName == core.GridStyle; return a; } 

に渡しているコールバックが戻っa、およびfindbooleanを返すコールバックを期待するためです。

あなたの質問にエラーがもともとそれが

const t: GridType = gridData.find(a => { a.GridName == core.GridStyle; }); 

として書かれたとあなたがreturn文なしで中括弧内のコードを持っているとき、voidを返すと仮定ているため、おそらくです

Type 'void' is not assignable to type 'boolean'. 

を言います。

特定のスタイルのグリッドを検索する必要がある場合は、比較結果a.GridName == code.GridStyleを返す必要があります。

const t: GridType = gridData.find(a => { return a.GridName == core.GridStyle; }); 
+0

は役に立ちました。はい、私はもともとそのように書いていましたし、その後、私はそれが動作していないと見て、私はそれを追加することを忘れて気づいたときに戻ってきた、ちょうど間違った場所に追加された笑。ご協力いただきありがとうございます – Optiq

関連する問題