Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Passing an immutable valueImmutable<T> to a function that expects a mutable parameterT#77

Answeredbyunadlib
alexamy asked this question inQ&A
Discussion options

I frequently pass values of typeImmutable<T> to functions that expect parameters of typeT, and this usually works without any typing errors. However, yesterday I encountered a case where this didn't work (see code below).

As a workaround, I avoid the issue by passing problematic types together withPrimitive | AtomicObject types to prevent theImmutable type from being applied to them in theImmutable type definition.

  1. What is the intended type relationship betweenImmutable<T> andT?
  2. Are they designed to be type-compatible, have I been observing some edge case or bug?
  3. Are there any better solutions to this problem?
// Expecting to pass `Immutable<HTMLElement>` as wellconstf=(element:HTMLElement)=>{};constel:Immutable<HTMLElement>=document.body;// TypeScript error:// The type 'readonly ImmutableObject<CSSStyleSheet>[]' is 'readonly' and cannot be assigned to the mutable type 'CSSStyleSheet[]'.f(el);
You must be logged in to vote

hi@alexamy,

Immutable is a strict immutable generic type that primarily uses TypeScript'sreadonly keyword to ensure properties are immutable, and it traverses all deep nested properties.

For stricter immutability, we do not recommend such compatibility.

As a workaround, you could also do it this way:

const f = (element: HTMLElement) => {};const el: Immutable<HTMLElement> = document.body;f(el as HTMLElement);

Replies: 1 comment

Comment options

hi@alexamy,

Immutable is a strict immutable generic type that primarily uses TypeScript'sreadonly keyword to ensure properties are immutable, and it traverses all deep nested properties.

For stricter immutability, we do not recommend such compatibility.

As a workaround, you could also do it this way:

const f = (element: HTMLElement) => {};const el: Immutable<HTMLElement> = document.body;f(el as HTMLElement);
You must be logged in to vote
0 replies
Answer selected byalexamy
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@alexamy@unadlib

[8]ページ先頭

©2009-2025 Movatter.jp