typescript - pass type to function parameter


LTFoReal

I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example.

interface ReturnObj<T> {
  returnObj: T | null
}

const createResultObj = (
    isSuccessful: boolean = false,
    returnObj: ReturnObj = null,
    errors: AppError | null = null,
    statusCode: number = 500
   ): Result => {
return {
       isSuccessful,
       returnObj,
       errors,
       statusCode
     }
}

Naturally, this will return an error, Generic type 'ReturnObj<T>' requires 1 type argument(s).but I'm not sure how to populate this part so that I can call in this function and include the type (if some type is present in returnObj).

Elda

All you need to do is provide a generic parameter to your function like this:

interface ReturnObj<T> {
  returnObj: T | null
}

type AppError = {} | null
type Result<T extends unknown> = {
  isSuccessful: boolean,
  returnObj: ReturnObj<T> | null,
  errors: AppError,
  statusCode: number
}

const createResultObj = <T extends unknown>(
  isSuccessful: boolean = false,
  returnObj: ReturnObj<T> | null = null,
  errors: AppError = null,
  statusCode: number = 500
): Result<T> => {
  return {
    isSuccessful,
    returnObj,
    errors,
    statusCode
  }
}

type sth = { foo: string };
const s: sth = { foo: "bar" }
const returnS: ReturnObj<sth> = { returnObj: s }
const re = createResultObj(true, returnS, null, 200);
console.log(re);

playground link

Note: This<T extends unknown> is an obligation due to the use of arrow functions, see this

Related


typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

typescript - pass type to function parameter

LTFoReal I'm pretty new to Typescript, but I'm trying to pass a custom type to a function. In this case I avoid "any", here is my example. interface ReturnObj<T> { returnObj: T | null } const createResultObj = ( isSuccessful: boolean = false, return

Typescript pass function as parameter

Joachim connectWebSocket() { const socket = new SockJS('http://localhost:8080/websocket'); this.stompClient = Stomp.over(socket); const _this = this; this.stompClient.connect({ "Authorization" : "Bearer "+localStorage.getItem("Authorization"

Pass a "type" parameter to a function

Jean Is it possible to pass typeparameters to a function so that create_eclass*the function can only be written once by just passing the class type parameter to the function? class bclass; virtual function void print(); $display("Base Class"); endfunction

Pass a "type" parameter to a function

Jean Is it possible to pass typeparameters to a function so that create_eclass*the function can only be written once by just passing the class type parameter to the function? class bclass; virtual function void print(); $display("Base Class"); endfunction

Pass a "type" parameter to a function

Jean Is it possible to pass typeparameters to a function so that create_eclass*the function can only be written once by just passing the class type parameter to the function? class bclass; virtual function void print(); $display("Base Class"); endfunction

Pass a "type" parameter to a function

Jean Is it possible to pass typeparameters to a function so that create_eclass*the function can only be written once by just passing the class type parameter to the function? class bclass; virtual function void print(); $display("Base Class"); endfunction

Pass a "type" parameter to a function

Jean Is it possible to pass typeparameters to a function so that create_eclass*the function can only be written once by just passing the class type parameter to the function? class bclass; virtual function void print(); $display("Base Class"); endfunction

Pass enum type as parameter in typescript

JᴀʏMᴇᴇ I have a function: public doSomethingWithEnum(enumType) { // Iterate over enum keys with Object.keys(enumType) } I can use it like this: export enum MyEnum { SomeEnumValue = 'SomeEnumValue', SomeOtherValue = 'SomeOtherValue' } doSomethingWithEnum(M

Pass enum type as parameter in typescript

JᴀʏMᴇᴇ I have a function: public doSomethingWithEnum(enumType) { // Iterate over enum keys with Object.keys(enumType) } I can use it like this: export enum MyEnum { SomeEnumValue = 'SomeEnumValue', SomeOtherValue = 'SomeOtherValue' } doSomethingWithEnum(M

Pass enum type as parameter in typescript

JᴀʏMᴇᴇ I have a function: public doSomethingWithEnum(enumType) { // Iterate over enum keys with Object.keys(enumType) } I can use it like this: export enum MyEnum { SomeEnumValue = 'SomeEnumValue', SomeOtherValue = 'SomeOtherValue' } doSomethingWithEnum(M

Pass enum type as parameter in typescript

JᴀʏMᴇᴇ I have a function: public doSomethingWithEnum(enumType) { // Iterate over enum keys with Object.keys(enumType) } I can use it like this: export enum MyEnum { SomeEnumValue = 'SomeEnumValue', SomeOtherValue = 'SomeOtherValue' } doSomethingWithEnum(M

Pass enum type as parameter in typescript

JᴀʏMᴇᴇ I have a function: public doSomethingWithEnum(enumType) { // Iterate over enum keys with Object.keys(enumType) } I can use it like this: export enum MyEnum { SomeEnumValue = 'SomeEnumValue', SomeOtherValue = 'SomeOtherValue' } doSomethingWithEnum(M

Pass enum type as parameter in typescript

JᴀʏMᴇᴇ I have a function: public doSomethingWithEnum(enumType) { // Iterate over enum keys with Object.keys(enumType) } I can use it like this: export enum MyEnum { SomeEnumValue = 'SomeEnumValue', SomeOtherValue = 'SomeOtherValue' } doSomethingWithEnum(M

TypeScript: Pass parameter to callback function

Chaos Zh The following code is from the source code of the azure-accountVSCode extension's code sample . export function activate(context: ExtensionContext) { const azureAccount = extensions.getExtension<AzureAccount>('ms-vscode.azure-account')!.exports;

How to pass a type parameter to an indexed type in Typescript?

Alex D I'm trying to figure out how to get the return type of a generic function in a class. E.g, declare class Test { open<T, R>(t1: T, t2: R): T | R; } type TestType<T, R> = (Test['open'])<T, R>; type TestTypeReturn = ReturnType<TestType<number, number

How to pass a type parameter to an indexed type in Typescript?

Alex D I'm trying to figure out how to get the return type of a generic function in a class. E.g, declare class Test { open<T, R>(t1: T, t2: R): T | R; } type TestType<T, R> = (Test['open'])<T, R>; type TestTypeReturn = ReturnType<TestType<number, number

How to pass a type parameter to an indexed type in Typescript?

Alex D I'm trying to figure out how to get the return type of a generic function in a class. E.g, declare class Test { open<T, R>(t1: T, t2: R): T | R; } type TestType<T, R> = (Test['open'])<T, R>; type TestTypeReturn = ReturnType<TestType<number, number

How to pass a type parameter to an indexed type in Typescript?

Alex D I'm trying to figure out how to get the return type of a generic function in a class. E.g, declare class Test { open<T, R>(t1: T, t2: R): T | R; } type TestType<T, R> = (Test['open'])<T, R>; type TestTypeReturn = ReturnType<TestType<number, number

How to pass a type as a function parameter

Isaac I have the following type checking code to verify that userInput is one of the defined values const variantColorValues = ['primary', 'black', 'green', 'red', 'white'] as const; export type VariantColor = (typeof variantColorValues)[number]; function isO

Javascript, pass type as parameter in function

math I'm currently learning Javascript and I'm finding some things that don't make much sense to me. In the example on the ArcGIS website there is this code var map require(["esri/map", "dojo/domReady!"], function(Map) { map = new Map("mapDiv", { center:

Pass data type as parameter to function

0x29a I am using GSON to parse JSON in the following way: var obj = Gson().fromJson( json, Array<MyDataModel>::class.java ).toList() I would like to be able to pass the data type through the method in the following way: fun convert( t : Any ) : Any { retu

Javascript, pass type as parameter in function

math I'm currently learning Javascript and I'm finding some things that don't make much sense to me. In the example on the ArcGIS website there is this code var map require(["esri/map", "dojo/domReady!"], function(Map) { map = new Map("mapDiv", { center:

How to pass a type as a function parameter

Isaac I have the following type checking code to verify that userInput is one of the defined values const variantColorValues = ['primary', 'black', 'green', 'red', 'white'] as const; export type VariantColor = (typeof variantColorValues)[number]; function isO

QBasic pass type as function parameter

West Berlin Going back to the old qbasic for nostalgic reasons and never used qbasic's types and functions since I was very young then. TYPE Vector2 x AS SINGLE y AS SINGLE END TYPE FUNCTION Vector2Mag (a AS Vector2) Vector2Mag = SQR((a.x * a.x) +

Javascript, pass type as parameter in function

math I'm currently learning Javascript and I'm finding some things that don't make much sense to me. In the example on the ArcGIS website there is this code var map require(["esri/map", "dojo/domReady!"], function(Map) { map = new Map("mapDiv", { center:

Javascript, pass type as parameter in function

math I'm currently learning Javascript and I'm finding some things that don't make much sense to me. In the example on the ArcGIS website there is this code var map require(["esri/map", "dojo/domReady!"], function(Map) { map = new Map("mapDiv", { center:

Javascript, pass type as parameter in function

math I'm currently learning Javascript and I'm finding some things that don't make much sense to me. In the example on the ArcGIS website there is this code var map require(["esri/map", "dojo/domReady!"], function(Map) { map = new Map("mapDiv", { center: