#Function overloading seperation

27 messages · Page 1 of 1 (latest)

undone wigeon
#

 import { FindOneOptions, FindOptionsWhere, ObjectLiteral } from "typeorm";
 
 export declare function findWhere<TEntity>(
   where: FindOptionsWhere<TEntity>,
   condition?: string,
   parameters?: ObjectLiteral
 ): Promise<TEntity[] | null>;
 
 export declare function findWhere<TEntity>(
   oneWhere: FindOneOptions<TEntity>,
   condition?: string,
   parameters?: ObjectLiteral
 ): Promise<TEntity | null>;

#

Basically these are functional declarations

#

I declared a function overload and tried to export import them I just wanted to make a seperate file for my types and my actual functions but i think thats not the case with function declaration

#

How can i declare the declarations in another file and use them in another file is it even possible?

#
async findOneWhere(
    oneWhere?: FindOneOptions<TEntity> | FindOptionsWhere<TEntity> | undefined
  ): Promise<TEntity | null> {
    if ("where" in oneWhere) {
      const where = oneWhere as FindOneOptions<TEntity>;
      return await this.providedRepo.findOne(where);
    }
    const where = oneWhere as FindOptionsWhere<TEntity>;
    return await this.providedRepo.findOne({ where });
  }

I want to make this function more cleaner with function overloading

#

From the overloads defined above

split jay
#

@undone wigeon FWIW, I would suggest not making separate files for types and runtime code.

#

You can declare an overload as a type:

type OverloadedFunctionType = {
   // signature 1
   (/*...args*/): ReturnType,
   // signature 2
   (/*...args*/): ReturnType,
}
#

... but this form is not as good as the normal function overload syntax.

undone wigeon
#

Someone suggested me to make a declaration.d.ts and put declaration inside @split jay is that good?

split jay
#

.d.ts files are for declaring types and so they're not really checked the way that normal TS files are.

#

I would suggest keeping your types and code together, both in this situation and as a general rule.

undone wigeon
#

Ok

#

Also im stuck in a situation

#

As you can see the function is taking 3 parameters in both cases and i was dumb bc i was applying the declaration instead I want to run a specific code on a specific parameter type

#

Only the one where function is of different type

#

The function declaration wont help this case i guess

#

And i cannot even compare library specific types in typescript for example
If onewhere === Findoneoptions.....
This is invalid , how can i run specific code on specific parameter type?

split jay
#

I'm not 100% sure what you're asking, but if you want the code to change based on the types, it can't.

#

TS is just a type-checker - at runtime it's stripped out and it's just JS code.

#

So you can only check types to the extent that JS supports at runtime: things typeof x === "object".

undone wigeon
#

Ohh oops

#

So i cant run specific code on specific parameter type i see bc types will be determined on run-time and there is no typescript at runtime right?

split jay
#

Yup. And TS doesn't do "type-directed emit".

#

When you compile TS to JS, it basically just strips out the typings.

undone wigeon
#

Gotcha