Answers for "typescript generic type"

3

typescript generic function

function firstElement<Type>(arr: Type[]): Type {
  return arr[0];
}
// s is of type 'string'
const s = firstElement(["a", "b", "c"]);
// n is of type 'number'
const n = firstElement([1, 2, 3]);
Posted by: Guest on April-25-2021
6

typescript class type t

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; };
Posted by: Guest on March-12-2020
1

typescript generic type

function identity<T>(arg: T): T {
  return arg;
}Try
Posted by: Guest on December-07-2020
-1

how to make a generic variable in javascript

var x = 1;
var y = 1;
if (x == y) {
  x = x + 1
}
Posted by: Guest on August-09-2020

Code answers related to "TypeScript"

Browse Popular Code Answers by Language