question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subsequent variable declarations must have the same type when trying to type `t.context` [TypeScript]

See original GitHub issue

Copied from StackOverflow

Description

I use ava with typescript and want to type ava’s test context. It’s typed as any in ava’s definition file.

@novemberborn suggested adding a generic to the test() signature

Test Source

What I specifically want is that the typescript compiler knows that t.context is of the type {foo: number} in the following test:

import test from 'ava'

test.beforeEach((t) => {
  t.context = { foo: 5 }
})

test('Is context typed', (t) => {
  // uncaught typo
  t.is(t.context.fooo, 5)
})

I tried to use declaration merging to do this, but it fails with TS2403: Subsequent variable declarations must have the same type. Variable 'context' must be of type 'any', but here has type '{ foo: number; }'. :

declare module 'ava' {
    interface ContextualTestContext {
      context: {
        foo: number,
      }
    }
}

test.beforeEach((t) => {
  t.context = { foo: 5 }
})

test('Is context typed', (t) => {
  // uncaught ypo
  t.is(t.context.fooo, 5)
})

Is there a way to do this without casting the context all the time like so:

interface IMyContext {
  foo: number
}

test.beforeEach((t) => {
  t.context = { foo: 5 }
})

test('Is context typed', (t) => {
  const context = <IMyContext> t.context
  // caught typo
  t.is(context.fooo, 5)
})

Error Message & Stack Trace

does not apply

Config

does not apply

Command-Line Arguments

does not apply

Relevant Links

Environment

Tell us which operating system you are using, as well as which versions of Node.js, npm, and AVA. Run the following to get it quickly:

Node.js v6.9.5
darwin 16.4.0
0.18.2
3.10.10

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
despairbluecommented, Mar 16, 2017

Nevermind changing the type-signature to actually reflect what’s going on fixes this just fine. The issue is closed. Thank you!

function contextualize<T>(getContext: () => Promise<T>): ava.RegisterContextual<T> {

0reactions
despairbluecommented, Mar 16, 2017

@mmkal Awesome work in this PR. I’m just still struggling to get this to work with async beforeEachs. Changing the type of T in the beforeEach is not picked up by the typescript compiler:

import * as ava from 'ava'
import * as mongodb from 'mongodb'

function contextualize<T>(getContext: () => T): ava.RegisterContextual<T> {
  ava.test.beforeEach(async (t) => {
    Object.assign(t.context, await getContext())
  })

  return ava.test
}

const test = contextualize(async () => {
  const db = await mongodb.MongoClient.connect('mongodb://localhost:27017')

  return { db }
})

test('foo', async (t) => {
  // fails with "TS2339: Property 'db' does not exist on type 'Promise<{ db: Db; }>'."
  // console.log(t.context.db.serverConfig)

  // works
  const context = (await t.context)
  console.log(context.db.serverConfig)

  // is true since `t.context` is not a promise
  console.log(t.context === <any> context) // true
})

I think it’s still ok to make each test async and await the context since it still saves me from defining an interface and casting the context.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Subsequent variable declarations must have the same type ...
Try using find-all-references on declare var require . It should show you another declare var require with a different type.
Read more >
Subsequent variable declarations must have the same type. any
It's typed as any in ava's definition file. What I specifically want is that the typescript compiler knows that t.context is of the...
Read more >
Subsequent variable declarations must have the same type ...
I'm getting this typescript error: error TS2403: Subsequent variable declarations must have the same type. Variable 'environment' must be of type 'string', ...
Read more >
Documentation - Everyday Types - TypeScript
In this chapter, we'll cover some of the most common types of values you'll find in JavaScript code, and explain the corresponding ways...
Read more >
Documentation - Advanced Types - TypeScript
To define a type guard, we simply need to define a function whose return type ... declare a variable, it doesn't automatically include...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found