`Exact` type doesn't protect against errors
See original GitHub issueAfter 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
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:7
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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/
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>
toValidateStructure<T, Struct>
but it would required a lot of work becauseValidateStructure<T, Struct>
requires Generic argument parameters whileExact<T>
did notDuplicate of #4577.