Search code examples
typescriptag-grid-angular

Trouble instantiating ColDef variable


I am struggling w creating a local variable in TypeScript. The ColDef is a type found in the agGrid.

I can create an array of ColDefs w/ just one entry and then grab that... But I can not seem to arrive at the correct syntax for simply

    import { ColDef } from 'ag-grid-community';

foo(){

    let try1 = new ColDef(  {field : this.fieldName}); //get error
    let try2 = new ColDef(   this.fieldName );  //get error 
    const try3 = new ColDef(   this.fieldName );  //get error 

   
    let myCDs : ColDef[] = [
            {field : this.fieldName}
          ];
    let myCD : ColDef = myCDs[0];  //and yet this works...  

}

regardless of what I try, i get

ERROR in src/app/helpers/agGrid/AgColDef.ts(20,31): error TS2693: 'ColDef' only refers to a type, but is being used as a value here.

What is the correct syntax to declare just 1 ColDef object?

(i realize this is a newbie TS question, so tyia)


Solution

  • You are trying to use an Interface Like a class.

    export interface ColDef extends AbstractColDef, IFilterDef ColDef github

    If you want to create one object for an interface:

    let obj: ColDef = { /* properties */ }
    

    This works because the created object just needs the specified set of properties in ColDef interface.