Skip to content

T[K] is not assignable to NonNullable<T>[K] even when T is nonnull (regression) #27456

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

Closed
falsandtru opened this issue Sep 29, 2018 · 4 comments
Labels
Needs More Info The issue still hasn't been fully clarified

Comments

@falsandtru
Copy link
Contributor

TypeScript Version: 3.2.0-dev.20180929

Search Terms:

Code

Revert the change of source code to see the error: falsandtru/clientchannel@14cc53a

Expected behavior:

pass

Actual behavior:

TypeScript error: src/layer/domain/indexeddb/service/channel.ts(48,15): Error TS2322: Type 'V[Exclude<keyof V, "__meta" | "__id" | "__key" | "__date" | "__event">] | undefined' is not assignable to type 'NonNullable<Partial<V>>[Exclude<keyof V, "__meta" | "__id" | "__key" | "__date" | "__event">]'.
  Type 'undefined' is not assignable to type 'NonNullable<Partial<V>>[Exclude<keyof V, "__meta" | "__id" | "__key" | "__date" | "__event">]'.

Playground Link:

Related Issues:

@ahejlsberg
Copy link
Member

I'm not sure there is a regression here. The following is an error with 3.0 as well:

// Compile with --strictNullChecks
function foo<T, K extends keyof T>(x: T[K], y: NonNullable<T>[K]) {
  x = y;
  y = x;  // Error
}

I think the error is reasonable. After all, NonNullable<T> is a subtype of T.

@ahejlsberg ahejlsberg added the Needs More Info The issue still hasn't been fully clarified label Sep 30, 2018
@falsandtru
Copy link
Contributor Author

Compiling passed probably since 2018-3-5.

https://github.com/falsandtru/clientchannel/blob/0611018e8b840abbcd7a9d49f08c9dc12daa847b/src/layer/domain/indexeddb/service/channel.ts
https://travis-ci.org/falsandtru/clientchannel/builds

Generally, subtype isn't replaceable by base type. But if T is originally nonnull, T and NonNullable<T> are the same type (base type is replaceable by subtype in this case). And if T is nullable, property access should be error. What do you think?

@falsandtru falsandtru changed the title T[K] is not assignable to NonNullable<T>[K] (regression) T[K] is not assignable to NonNullable<T>[K] even when T is originally nonnull (regression) Sep 30, 2018
@falsandtru falsandtru changed the title T[K] is not assignable to NonNullable<T>[K] even when T is originally nonnull (regression) T[K] is not assignable to NonNullable<T>[K] even when T is nonnull (regression) Oct 1, 2018
@falsandtru
Copy link
Contributor Author

@ahejlsberg Can you take a look at the requested info?

@falsandtru
Copy link
Contributor Author

@ahejlsberg

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs More Info The issue still hasn't been fully clarified
Projects
None yet
Development

No branches or pull requests

3 participants