baconjs / bacon.js

Functional reactive programming library for TypeScript and JavaScript

Home Page:https://baconjs.github.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'Property<string>' is not assignable to type 'Property<string | null>'

kaisellgren opened this issue · comments

Given the following code:

const test: Bacon.Property<string | null> = Bacon.constant('')

For some reason when using strict mode in TypeScript, I get this type error:

TS2322: Type 'Property<string>' is not assignable to type 'Property<string | null>'.  
The types of 'changes().bufferWithTime(...).bufferWithTime(...).bufferWithTime(...).concat' are incompatible between these types. 
Type '{ (other: Observable<string[][][]>, options?: EventStreamOptions | undefined): EventStream<string[][][]>; <V2>(other: Observable<V2>, options?: EventStreamOptions | undefined): EventStream<...>; }' is not assignable to type '{ (other: Observable<(string | null)[][][]>, options?: EventStreamOptions | undefined): EventStream<(string | null)[][][]>; <V2>(other: Observable<V2>, options?: EventStreamOptions | undefined): EventStream<...>; }'.      
Types of parameters 'other' and 'other' are incompatible.     
Type 'Observable<(string | null)[][][]>' is not assignable to type 'Observable<string[][][]>'. 
Types of property 'combine' are incompatible.      
Type '<V2, R>(right: Observable<V2>, f: Function2<(string | null)[][][], V2, R>) => Property<R>' is not assignable to type '<V2, R>(right: Observable<V2>, f: Function2<string[][][], V2, R>) => Property<R>'.    
Types of parameters 'f' and 'f' are incompatible.        
Types of parameters 't1' and 't1' are incompatible.    
Type '(string | null)[][][]' is not assignable to type 'string[][][]'.

This does not happen with this test code:

class Test<T> {
  constructor(value: T) {}
}

const test: Test<string | null> = new Test('')

I am not sure where this goes wrong, but string should be assignable to string | null including with generics.

And here's another way to reproduce this:

const test: Bacon.Property<boolean> = Bacon.constant(true)

Apparently, true cannot be assigned to boolean when wrapped within a Bacon object.

I am using TypeScript 4.0.2

@raimohanska
Is there a fix for this? This is quite annoying and prevents from writing simple TypeScript.

This shouldn't cause a type error:

const test: Bacon.Property<boolean> = Bacon.constant(true)

In regular TypeScript code this works fine:

interface Generic<T> {
  a: T
}

const a: boolean = true
const b: Generic<boolean> = { a: true }

You may supply the type parameter explicitly like this:

const test = Bacon.constant<string | null>("");
const test = Bacon.constant<boolean>(false);

I guess this depends on TypeScript version and options, but for me, constant(true) seems to create a Property<boolean>:

image

A team member (Petri Väkeväinen) has informed me that with the latest version of TypeScript it appears to work just fine.

I'm closing this issue now. Thanks.