Skip to content

in 2.1.4, strictNullChecks make index types of nullable fields be never #12747

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
scottbessler opened this issue Dec 8, 2016 · 4 comments
Closed
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@scottbessler
Copy link

scottbessler commented Dec 8, 2016

TypeScript Version: 2.1.4

Code

function get<T, K extends keyof T, K2 extends keyof T[K]>(obj: T, propertyName: K, propertyName2: K2): T[K][K2] | undefined {
    const step1 = obj ? obj[propertyName] : undefined;
    return step1 ? step1[propertyName2] : undefined;
}

interface Org {
    admin?: User;
}
interface User {
    email?: string;
}

const o: Org = { admin: { email: 'foo' } };
const o2: Org = {};

console.log(get(o, 'admin', 'email'))
console.log(get(o2, 'admin', 'email'))

Expected behavior:

Expect to output:

foo
undefined

Actual behavior:
with strictNullChecks, fails to compile:

foo.ts(17,29): error TS2345: Argument of type '"email"' is not assignable to parameter of type 'never'.
foo.ts(18,30): error TS2345: Argument of type '"email"' is not assignable to parameter of type 'never'.

works without strictNullChecks:

foo
undefined
@mhegazy
Copy link
Contributor

mhegazy commented Dec 8, 2016

Duplicate of #12651, #12723, and #12744.

fixed by #12770

@mhegazy mhegazy added the Duplicate An existing issue was already created label Dec 8, 2016
@ahejlsberg
Copy link
Member

ahejlsberg commented Dec 8, 2016

Actually, this isn't a duplicate, but more like a suggestion that we somehow permit indexed access types to work on types that include undefined and/or null. It would probably require a new type operator, e.g. T!, which removes undefined and null from the type of T. We could then say that when a truthy, undefined, or null check is applied to an expression of a generic type T, we produce the type T! in order to record the knowledge that the type is not nullable.

Anyway, I'm going to reopen and change to a suggestion.

@ahejlsberg ahejlsberg reopened this Dec 8, 2016
@ahejlsberg ahejlsberg added Suggestion An idea for TypeScript and removed Duplicate An existing issue was already created labels Dec 8, 2016
@RyanCavanaugh RyanCavanaugh added the In Discussion Not yet reached consensus label Dec 8, 2016
@scottbessler
Copy link
Author

Ah yeah i was wondering if this was expected behavior.

T! or somesuch would be great.

@RyanCavanaugh
Copy link
Member

Closing due to low feedback volume.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants