Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Value declared with interface not assignable to Record<string, unknown> #60677

Closed
MarkDuckworth opened this issue Dec 4, 2024 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@MarkDuckworth
Copy link

🔎 Search Terms

"T extends Record<string, unknown>", "interface extends Record<string, unknown>", "interface not assignable to Record<string, unknown>

🕗 Version & Regression Information

  • This is the behavior in every version I tried (^3.x, ^4.x, ^5.x), and I reviewed the FAQ for entries about types vs interfaces, conditional types, and unknown type

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.2&ssl=8&ssc=38&pln=8&pc=1#code/PTAEBUFMGcBcEsB2BzUsDuB7U8AmlEEBjAQwBtRoALEgBxgC5QBZAT3FftBMVxdYCShSACcAZiSKQAULE6R+HLgF5QAb1BjMmJgCNtZSD1ABfaUliiJU-kMvjJCjVp2h9mQ8bPSQEGLG5oaHhkRCRUWGwAJUgiTBFcAB44EXCAGlAAV0QAa0RMdEQAPmk4xDhuJjY7K0dQVWdtJlgRTIUTAG4fMAAxEngyaCYlBQByauEHKVGcaFB8gJIgkMQSXUM0bDkuUZi4hOSW9Kzc-MKi0dLMcoDdJj34pJTj7LyC4vruLqub0CIq9jyT6NVwtNqmLplCq4e6xR6HVIoDKvM4fVREb6+KBwcIQUCQAAell4cweB2eSJOb3OsiBAmgZNwAGUjigAKqnd6JcBFT7gfFEgi4Ulw8ms5DIznnUAAflAo1YMBmTFG+VGkOuFUgTHpjJZiOQHOpiESbBGvNUCqVXV8AEE5iRQCIYJkyLAMhN7NYFLhMDB5pgAoTiXxGQiXlLij8KmIdQzRczxUbUabBJNvRb5WrvtJc7nfMpC4XQLbcLh4AhruQ0P45rpIGQCvUi8powFkHoDEZEAKQ3MUe9ZfLFdBlVnMDNLSP1W3QFQATUppAANqjFyjAC6vaF-cjQ6to9AKrVnwPM+6fhxKDxwZ3oDDFIl3EQrBK2wUuoT+vCtpf3Mz-K3iS94JuGlI8K++7Tke44zlCATwHGerir+rCpuap7ThqvwAFZIV+KF-p6tRSJmZ4dEAA

💻 Code

// Testing two identical shapes: MyType and MyInterface
type MyType = { foo: boolean }
interface MyInterface { foo: boolean }

// Test assigning to Record<string, unknown>
const a: MyInterface = { foo: true };
// Fails: Type 'MyInterface' is not assignable to type 'Record<string, unknown>'
const b: Record<string, unknown> = a;

// This is fine when the declaration uses MyType
const c: MyType = { foo: true };
const d: Record<string, unknown> = c;

// Testing T extends Record<string, unknown>
type IsRecordStringUnknown<T> = T extends Record<string, unknown> ? 'yes' : 'no';
const e: IsRecordStringUnknown<MyType> = 'yes';
// Similarly, MyInterface does not extend Record<string, unknown>
const f: IsRecordStringUnknown<MyInterface> = 'no';

🙁 Actual behavior

  • Type MyInterface is not assignable to type Record<string, unknown>
  • MyInterface does not extend Record<string, unknown>.

🙂 Expected behavior

MyInterface is assignable to Record<string, unknown>, so the following code should compile:

const a: MyInterface = { foo: true };
const b: Record<string, unknown> = a;

MyInterface extends Record<string, unknown>, so the following code should compile:

const d: IsRecordStringUnknown<MyInterface> = 'yes';

Additional information about the issue

The most relevant documentation I could find related to this:

// Anything but primitive assignable to { [x: string]: unknown }
function f24(x: { [x: string]: unknown }) {
  x = {};
  x = { a: 5 };
  x = [1, 2, 3];
  x = 123; // Error
}

source

@MartinJohns
Copy link
Contributor

This is working as intended. interface and type are not interchangeably.

Duplicate of #42825. See also: #15300 (comment)

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 4, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Dec 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants