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.

`Exact` type doesn't protect against errors

See original GitHub issue

After this issue https://github.com/dotansimha/graphql-code-generator/issues/4120 in generated files appears Exact type

type Exact<T extends { [key: string]: unknown }> = {
  [K in keyof T]: T[K];
};

that doesn’t do anything

Example 1:

type Variables = { [key: string]: never }

declare const query: (variables: QueryVariables) => void

query({}) // Ok
query({a: 1}) // Argument of type '{ a: number; }' is not assignable to parameter of type 'Variables'

vs Exact version

type Variables = { [key: string]: never }

declare const query: (variables: Exact<Variables>) => void

query({}) // Ok
query({a: 1}) // Argument of type '{ a: number; }' is not assignable to parameter of type 'Variables'

It works absolutely the same

Example 2:

type Variables = { email: string }

declare const query: (variables: Variables) => void

query({email: 'email', password: 'password'}) // Argument of type '{ email: string; password: string; }' is not assignable to parameter of type 'Variables'.

const variables = {email: 'email', password: 'password'}
query(variables ) // Ok

I can pass additional fields and have no errors in TS, but have errors in runtime vs Exact version

type Variables = { email: string }

declare const query: (variables: Exact<Variables>) => void

query({email: 'email', password: 'password'}) // Argument of type '{ email: string; password: string; }' is not assignable to parameter of type 'Variables'.

const variables = {email: 'email', password: 'password'}
query(variables ) // Ok

It works absolutely the same and doesn’t protect against errors

Link to the TS playground

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:1
  • Comments:7

github_iconTop GitHub Comments

1reaction
charlypolycommented, Apr 7, 2022

Hi @oceandrama,

Yes, it seems that the Exact<T> type helper is kind of a noop.

After doing some research, I found this famous implementation that does the work: https://fettblog.eu/typescript-match-the-exact-object-shape/

type ValidateStructure<T, Struct> = 
  T extends Struct ? 
  Exclude<keyof T, keyof Struct> extends never ? T : never : never;

Playground example from the article

This is the solution mentioned many times in the official TypeScript issue for “Exact Types”: https://github.com/microsoft/TypeScript/issues/12936

I guess we could migrate from Exact<T> to ValidateStructure<T, Struct> but it would required a lot of work because ValidateStructure<T, Struct> requires Generic argument parameters while Exact<T> did not

0reactions
tv42commented, May 30, 2022

Duplicate of #4577.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type II Error Explained, Plus Example & vs. Type I Error
A type II error is defined as the probability of incorrectly failing to reject the null hypothesis, when in fact it is not...
Read more >
How to correct a #N/A error in the VLOOKUP function
Problem: The lookup value is not in the first column in the table_array argument · Consider using INDEX/MATCH instead · Problem: The exact...
Read more >
DNA Replication and Causes of Mutation - Nature
Although DNA usually replicates with fairly high fidelity, mistakes do happen. The majority of these mistakes are corrected through DNA repair processes.
Read more >
Medication Dispensing Errors And Prevention - NCBI - NIH
A significant number of healthcare providers in the United States are from foreign countries and often write orders for medications that are not...
Read more >
Documentation - Narrowing - TypeScript
Argument of type 'string | number' is not assignable to parameter of type ... In TypeScript, checking against the value returned by typeof...
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