#extending global interface in typescript if anyone

1 messages · Page 1 of 1 (latest)

rotund sphinx
#

the problem is i'm using jest to test some functionalities and i want to do a global setup to log user and initialize prisma connection and use them across the tests, but when a extend global interface visual code says everything is okey but i get error: has no index signature
i mad a simple repo for the issue is anyone could help please:
u can just run yarn test to check the errors
https://github.com/Anas-Qadil/issueTS/tree/main/__tests__

GitHub

Contribute to Anas-Qadil/issueTS development by creating an account on GitHub.

radiant herald
#

multiple things

#
  1. about your tsconfig
#
  • you don't need the paths setting in your case, it doesn't do what you think it does, so can (and should) remove it
#
  • in the include property, "jest.config.ts" is already included in the **/* pattern, you can remove "jest.config.ts"
#
  • for the include property, you don't need to include everything, only include what you need, basically ["src/**/*.ts", "__tests__/**/*.ts", "jest.config.ts"] (also I don't think you need to include jest.config.ts but it can stay for now)
#
  • since you nbow included only what you need, you don't need to exclude the node_modules folder, you can remove the "exclude" property
#
  1. folder naming conventions
  • naming your test folder __tests__ isn't common, I'd suggest using the more common test or tests name
#
  1. typings
#
  • in your __tests__/globals.d.ts, you should use the exact types for your objects, don't use any, it provides no information at all, and might cause problem further in your code; you can always import the types you need from other TS files
    that's why you get this error: // this gives error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.
#
  • still in __tests__/globals.d.ts, don't use var, instead, use the more modern let or const keywords, especially if that global variable is a constant and can't be reassigned later
#

@rotund sphinx

rotund sphinx
#

@radiant herald i created a basic ts repo and most of configs came with it names, ts/jest configs ...
i'm using all of this to avoid any cz i can just use (global as any).user = "user". i used it there just to make the matter clear
i tried everything o stack overflow gpt everithing and none solved this samll problem

  • using let instead of var turns everything red
fervent sail
radiant herald
#

but declaring a let works just like declaring a var, in the end, it's the already existing variable that matters, TS declaration files don't create any variables

fervent sail
#

if it's being assigned as a global what does it matter

rotund sphinx
#

xD can anyone fix the main problem that i cant assign values to the vars inside the global

fervent sail
#

why does it matter if he uses var or let

fervent sail
radiant herald
fervent sail
#

ah, my mistake then

#

i just saw globals.d.ts, sorry

#

well it's not an async function

radiant herald
# fervent sail why does it matter if he uses var or let

it's not between var and let, it's between var and const

var gives no information about if the variable a constant or not

const tells the variable is a constant and cannot be reassigned
and since you are using const for constants, you might as well use let form mutable variables

fervent sail
#

I just use let for everything personally, just a matter of preference

#

i get your point though

#

most people like const

rotund sphinx
#

if i use let or const visual code yell saying : Property 'user' does not exist on type 'typeof globalThis'
while using var its good but there is a runtime error

#

and i saw some stackoverflow blogs saying in my case i have to use var

radiant herald
#

Property 'user' does not exist on type 'typeof globalThis'
it means your definition file isn't included correctly

fervent sail
#

where do you call that global-setup function

radiant herald
rotund sphinx
radiant herald
#

but tbh, don't use global variables that way

fervent sail
#

its supposed to be async? I dont know it that well

radiant herald
#

don't add anything to the global scope, you don't need to

fervent sail
#

just seems weird

rotund sphinx
radiant herald
#

you can jusy create variables and export them

fervent sail
#

const globalSetup = async () => {

// this gives error: Element implicitly has an 'any' type because type 'typeof globalThis' has no index signature.
global.user = "user";
global.agentRole = "agentRole";
global.role = "role";
global.prisma = "prisma";

};

#

im wondering why this is async

rotund sphinx
fervent sail
#

you're sure it makes no difference? cause you're not doing anything asynchronous there. I'm just trying to eliminate anything weird 1 by 1

rotund sphinx
fervent sail
#

try changing this to explicitly include `./tests/globals.d.ts'

rotund sphinx
radiant herald
#

also, if you read the issue, the type not found is for the global object

radiant herald
#

not the typings

fervent sail
#

will ** include 0 intermediate directories?

radiant herald
#

can you try adding a lib?

fervent sail
#

i can never keep all the damn glob patterns straight

radiant herald
rotund sphinx
radiant herald
#

"lib": ["ES6"]

fervent sail
#

that's helpful, thank you. I remember running into a problem with ** requiring at least one subdirectory with some glob syntax, that's good to know it doesnt in TS

rotund sphinx
#

i kinda found a way around:
i created another file called type.ts i declared the type there and imported it in global-setup.
and it works

#

but idk it doesnt in the previous case

#

it has someting to do with file name, when i remove .d and use just global.ts it works. but why?

fervent sail
#

Configuring Jest
The Jest philosophy is to work great by default, but sometimes you just need more configuration power.

It is recommended to define the configuration in a dedicated JavaScript, TypeScript or JSON file. The file will be discovered automatically, if it is named jest.config.js|ts|mjs|cjs|json. You can use --config flag to pass an explicit path to the file.

#

import type {Config} from 'jest';

const config: Config = {
globals: {
DEV: true,
},
};

export default config;

#

my 2c is it thinks you're trying to add global variables to your entire NODE app but really you just want them to apply to the test env

#

which seems to take a particular syntax and filename setup

#

Any global variables that are defined through globalSetup can only be read in globalTeardown. You cannot retrieve globals defined here in your test suites.