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.

Typescript error with required properties

See original GitHub issue

It seems like AllowImplicit is confusing Typescript into thinking that required properties are optional.

interface A {
  a: string;
}

const aDecoder: Decoder<A> = object({
  a: string
});

Output:

Type 'Decoder<{ a?: string; }, unknown>' is not assignable to type 'Decoder<A, unknown>'.
  Type '{ a?: string; }' is not assignable to type 'A'.
    Property 'a' is optional in type '{ a?: string; }' but required in type 'A'.ts(2322)

Live example: https://codesandbox.io/s/nostalgic-sinoussi-lkrq1?file=/src/index.ts

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
djlaukcommented, Oct 29, 2021

I narrowed it down to the strict setting in tsconfig.json.

  • when "strict": false (the default, when not set), the problem exists
  • when "strict": true the problem is gone

Steps to reproduce

Step 1: Create npm project

cd /tmp
mkdir repro-decoders-750
cd repro-decoders-750
npm init -y
npm i -D typescript@4.4.4
npm i decoders@1.25.4

Step 2: Create source file

Create file repro.ts:

import { object, string } from "decoders";
import type { Decoder } from "decoders";

export interface Person {
  name: string;
}

export const personDecoder: Decoder<Person> = object({
  name: string,
});

Step 3: Create tsconfig.json files

Create tsconfig.bad.json:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "es2020",
    "lib": ["es2020", "DOM"],
    "target": "es2020",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "strict": false
  }
}

Create tsconfig.ok.json:

{
  "compilerOptions": {
    "moduleResolution": "node",
    "module": "es2020",
    "lib": ["es2020", "DOM"],
    "target": "es2020",
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "strict": true
  }
}

Step 4: Demonstrate behaviour

Demostrate different behaviour:

npx tsc --noemit -p tsconfig.ok.json
# no output = no compiler errors = all ok

npx tsc --noemit -p tsconfig.bad.json
repro.ts:8:14 - error TS2322: Type 'Decoder<{ name?: string; }, unknown>' is not assignable to type 'Decoder<Person, unknown>'.
  Type '{ name?: string; }' is not assignable to type 'Person'.
    Property 'name' is optional in type '{ name?: string; }' but required in type 'Person'.

8 export const personDecoder: Decoder<Person> = object({
               ~~~~~~~~~~~~~


Found 1 error.

See difference in config:

diff -u tsconfig.*
--- tsconfig.bad.json	2021-10-29 15:37:58.000000000 +0200
+++ tsconfig.ok.json	2021-10-29 15:38:00.000000000 +0200
@@ -8,6 +8,6 @@
     "esModuleInterop": true,
     "skipLibCheck": true,
     "forceConsistentCasingInFileNames": true,
-    "strict": false
+    "strict": true
   }
 }
0reactions
stevekrousecommented, Nov 1, 2021

Thanks!!

Read more comments on GitHub >

github_iconTop Results From Across the Web

node.js - Typescript error when adding property to Error object ...
TypeScript does not allow adding unknown properties. There are ways to define objects with arbitrary keys (e.g. Record ).
Read more >
required properties produce a typescript error #2936 - GitHub
I was able to fix this issue by creating a new tscsonfig file in my entities folder, which includes "strictPropertyInitialization": false .
Read more >
Error - JavaScript - MDN Web Docs - Mozilla
Error objects are thrown when runtime errors occur. The Error object can also be used as a base object for user-defined exceptions.
Read more >
Handbook - Interfaces - TypeScript
Not all properties of an interface may be required. Some exist under certain conditions or ... Error: Property 'clor' does not exist on...
Read more >
React+TypeScript: 'missing required attribute' error reported ...
React+TypeScript: 'missing required attribute' error reported for required properties passed in by redux `connect` ... I would expect this to work. However ...
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