Tags down


Assignment involving generic property of generic object fails to typecheck correctly within generic function

By : Aldrin Ilagan
Date : October 18 2020, 06:10 PM
This might help you The first option (using KeysOfPropertiesWithType) does not work because typescript can't reason about conditional types that still contains unresolved type parameters (such as T and K in this example)
The second option does not work because T extends Record means T can for example be { a: false } which would mean the assignment obj[key] = true would not be valid. Generally, the fact that T[K] must extends a type does not mean that inside the generic function we can assign any value to it, the constraint just tells us what the minimum requirement is for the value, we don't yet know the full contract T[K] requires.
code :
function booleanAssignmentTest2<K extends PropertyKey>(obj: Record<K, boolean>, key: K): void {
    let foo: boolean = obj[key]; // Fine!
    let foo2: string = obj[key]; // Error: working as intended!
    obj[key] = true; // Ok now we know T[K] is boolean

let foo = { aBool: false, aNumber: 33, anotherBool: false };

booleanAssignmentTest2(foo, "aBool"); // Fine!
booleanAssignmentTest2(foo, "anotherBool"); // Fine!
booleanAssignmentTest2(foo, "aNumber"); // Error: working as intended!
function booleanAssignmentTest2<T extends Record<K, boolean>, K extends keyof T>(obj: T, key: K): void {
    let foo: boolean = obj[key]; // Fine!
    let foo2: string = obj[key]; // Error: working as intended!
    obj[key] = true as T[K]; // ok now

Share : facebook icon twitter icon

Generic class fails to accept object with direct line to the generic where type restriction

By : hobby
Date : March 29 2020, 07:55 AM
should help you out I have a c# data class defined with an inheritance chain that looks like this: , The following code seems to work for me.
code :
public class Screen { }
public abstract class EntityBaseCore
{    }

public abstract class EntityBase : EntityBaseCore
{    }
public partial class AdmSite : EntityBase
{    }

public abstract class ViewModelSecurityBase<T> : Screen where T : EntityBaseCore
{    }
public abstract class EditorViewModelBase<T> : ViewModelSecurityBase<T> where T : EntityBaseCore
{    }

public class SiteViewModel : EditorViewModelBase<AdmSite>
{    }

How to typecheck a generic function using a value of that same generic type in Flow

By : user1523679
Date : March 29 2020, 07:55 AM
I hope this helps you . for this very interesting question! Unfortunately I think that right now it is not possible to refine the type T by refining the type of the first element in the array. This can be seen when we use the following statement:
code :
if (firstElement) {
  if (typeof firstElement.id === 'string') {
    const refined: string = firstElement.id;
    const notRefined: T = refined; // Errors
    const alwaysWorks: T = firstElement.id;
  const alwaysWorks: T = firstElement.id;

How can get TypeScript to correctly type check call to a generic function that in turns calls a function with a generic

By : user2419118
Date : March 29 2020, 07:55 AM
it should still fix some issue I am trying to write code very similar to this: , Lift T parameter to outer func.
code :
const a = (m: <T>() => T) => { 
  return m();
// Type 'string' is not assignable to type 'T'
a(() => 'Hey!');
const a = <T>(m: () => T) => { 
  return m();
// Ok!
a(() => 'Hey!');
const useSearchState = <T>(mapState?: (state: SearchState) => T) => {
  if (mapState) {
    //                       Type 'T' is not assignable to type 'HasSearchState'.   
    return useHasSearchState((hasState) => mapState(hasState.searchState));
  return useHasSearchState((hasState) => hasState.searchState);
const useSearchState = <T extends HasSearchState>(mapState?: (state: SearchState) => T) => {
// Type 'string' is not assignable to type 'HasSearchState'.
const useId = () => useSearchState(state => state.id);

Constraint generic function by comparing instance property type with generic parameter

By : Bohao Cao
Date : March 29 2020, 07:55 AM
it helps some times What you are requesting is some kind of logical OR on type constraints:
code :
... where D == HasFirstDependency || HasSecondDependency

How do I create a generic Scala function that returns an object based on the generic type?

Date : March 29 2020, 07:55 AM
hop of those help? Take a look at Scala "manifests". They often allow you to circumvent type erasure on the JVM.
Related Posts Related Posts :
  • How do I add a counter to show how many times a div element has been clicked?
  • Calling A function using vue.js components and properties
  • How to check type of "typeof" in JavaScript?
  • Recursive function to determine if number is even or odd by subtracting two from n until n = 0 or 1
  • How to fix:"Refused to run the JavaScript URL because it violates the following Content Security Policy..."
  • Can't access value inside of props object
  • How map and filter over array of objects?
  • How to invert d3 color ramps d3-scale-chromatic d3.interpolateViridis
  • Comparing two variables JavaScript
  • Google Pay & Web Payment Request API:
  • How to pass date as parameter in javascript function
  • why i have this error "Expected an assignment or function call and instead saw an expression no-unused-expressions&
  • calculate the full turns of a circle
  • Applying spread operator on object with getter
  • How do I hide a parent div when child div contains a certain text?
  • How to Stream Live Audio With Low Latency
  • React: How to close a modal from child opened from parent component
  • how to find tab close and clear local storage
  • Function returning undefined value, a bit confused
  • What is output for foo.x
  • Remove singular element from an object's key array
  • How to fix lag issue over time in my Three.js game
  • async await promise.all map not resolving promises
  • Print an HTML document to PDF using JavaScript
  • Getting illegal invocation, after using $.ajax inside of $(doocument).keypress function
  • DebounceTime emits all the events which was captred during the time
  • Mongoose 5.x disallows passing a spread of operators
  • javascript onkeypress limit characters when pasting
  • How to fire an event only if specific-time has lapsed after scrolling has been stopped
  • How to filter an array of objects based on another array of objects?
  • Best way to switch between two colours with javascript?
  • Calling asynchronous function but returning result synchronously
  • Can't inject custom repository
  • How do I fix the Cordova error: Failed to fetch platform <<<<< HEAD?
  • Javascript: Store a global variable for a single page application
  • Node: Load/read the content of a page from external website(domain)
  • What is the difference between Object and object type
  • How to detect focusout on parent div not on child div in Angular4
  • How to end program after right answer?
  • How to use dot notation after +
  • Error from anonymous function: unexpected token <
  • Refresh a particular component in Angular
  • RegEx for validating three alphanumeric conditions
  • How to upload html file in the google and let it view by everyone?
  • Use Array.map on an 2-dimensional Array
  • Not sure whether I have syntax errors or errors with the code
  • How to fix process.nextTick is not a function with Calgolia places.js?
  • React Text Annotation
  • Is it possible to change the input type of an input element inside a <td> using <td> id with the use of java
  • Render functional component on variable change
  • Using Javascript to write a function which compares two trees and returns true if they are equal in structure and in val
  • How to save results from dynamic cascading drop down list?
  • How to pass arguments to function in functional component in React
  • Is there a way to view the contents of a predefined function like alert()?
  • object use own method in javascript
  • How do I replace NaN with 0 when doing division in javascript?
  • How to separate input field values in React?
  • How to link two JavaScript files together? NO HTML
  • Angular 7 - how to make initial input value visible for JSON?
  • How to get previous state value in reactjs
  • shadow
    Privacy Policy - Terms - Contact Us © 35dp-dentalpractice.co.uk