Type 'Type' is not generic

Asked
Active3 hr before
Viewed126 times

7 Answers

generic
90%

Just like before, these generic type parameters are “in scope” for that particular function — they are not class, or interface, or type alias-wide, They live only within the particular function upon which they’re specified

Example_snippet/controller/utility/_generic.js/ // Here’s the magic. `TypedLis. . .
// Here’s the magic. `TypedList` will operate on objects
// of type `User` due to the `<User>` syntax.
const userList = TypedList.create<User>([new User('Jamie')]);

// The compiler expects this.
userList.add(new User('Tom'));
userList.add(new User('Alice'));

// Argument of type '0' is not assignable to parameter 
// of type 'User'. ts(2345)
userList.add(0);

// Property 'getNames' does not exist on type 'User'. 
// Did you mean 'getName'? ts(2551)
// Note: TypeScript infers the type of `users` to be
// `TypedList<User>`
const users = userList.where(user => user.getNames() === 'Jamie');

// Property 'ssn' does not exist on type 'User'. ts(2339)
users.toArray()[0].ssn = '000000000';

// TypeScript infers `usersWithId` to be of type
// `TypedList<`{ id: string, name: string }>
const usersWithId = userList.select(user => ({
    id: newUuid(),
    name: user.getName()
}));

// Property 'ID' does not exist on type '{ id: string; name: string; }'. 
// Did you mean 'id'? ts(2551)
console.log(usersWithId.toArray()[0].ID)
load more v
88%

A generic type is a generic class or interface that is parameterized over types, The following Box class will be modified to demonstrate the concept

Example_snippet/controller/utility/_generic.js/ public class Box { private. . .
public class Box {
   private Object object;

   public void set(Object object) {
      this.object = object;
   }
   public Object get() {
      return object;
   }
}
load more v
72%

A generic type says "I'm not a generic type",,TypeScript Version: 3

Example_snippet/controller/utility/_generic.js/ interface IJsonable<T> { t. . .
interface IJsonable<T>
{
    toJSON(): T;
}

type Primitive<Instance> = value_of<Instance> extends object
    ? Instance extends IJsonable<infer Raw>
        ? Primitive<Raw>
        : PrimitiveObject<Instance>
    : value_of<Instance>;

type PrimitiveObject<Instance> =
{
    [P in keyof Instance]: Instance[P] extends Function
        ? never
        : Primitive<Instance[P]>
};

type value_of<Instance> = 
    Instance extends Boolean ? boolean
    : Instance extends Number ? number
    : Instance extends String ? string
    : Instance extends BigInt ? bigint
    : Instance;
load more v
65%

For export class GetAllConfigurations implements IReturn<Array<Configuration>> the Type 'Array' is not generic error is highlighted,Array<Configuration>> is highlighted with [ts] Type 'Array' is not generic in VS Code and after doing npm run build is returned as an error,,For createResponse() { return new Array<Configuration>(); } the Expected 0 type arguments, but got 1 is highlighted

Example_snippet/controller/utility/_generic.js/ [Route("/{OrgSlug}/configurati. . .
[Route("/{OrgSlug}/configurations", Verbs = "GET")]
public class GetAllConfigurations : IReturn<Configuration[]>
{
    public string OrgSlug { get; set; }
}
Step 2 continued with // @Route("/{OrgSlug}/configur. . .
// @Route("/{OrgSlug}/configurations", "GET")
export class GetAllConfigurations implements IReturn<Array<Configuration>>
{
    orgSlug: string;
    createResponse() { return new Array<Configuration>(); }
    getTypeName() { return "GetAllConfigurations"; }
}
load more v
75%

The generic class can also implement a generic interface, Consider the following example

Example_snippet/controller/utility/_generic.js/ class KeyValuePair<T,U> { . . .
class KeyValuePair<T,U>
{ 
    private key: T;
    private val: U;

    setKeyValue(key: T, val: U): void { 
        this.key = key;
        this.val = val;
    }

    display():void { 
        console.log(`Key = ${this.key}, val = ${this.val}`);
    }
}

let kvp1 = new KeyValuePair<number, string>();
kvp1.setKeyValue(1, "Steve");
kvp1.display(); //Output: Key = 1, Val = Steve 

let kvp2 = new KayValuePair<string, string>();
kvp2.SetKeyValue("CEO", "Bill"); 
kvp2.display(); //Output: Key = CEO, Val = Bill
load more v
40%

Search Answer Titles

Example_snippet/controller/utility/_model.js/ class GenericNumber<T> { z. . .
class GenericNumber<T> {
    zeroValue: T;
    add: (x: T, y: T) => T;
}

let myGenericNumber = new GenericNumber<number>();
myGenericNumber.zeroValue = 0;
myGenericNumber.add = function(x, y) { return x + y; };
22%

Update: Sorry, didn't notice you have Test as interface, It's fine too

Example_snippet/controller/utility/_model.js/ export class Test { field1: . . .
export class Test {
   field1: number;
   field2: string;
   /// ...
}
load more v