Skip to content

Possibly bug, TS wrongly complains that generic property is null #30205

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
al6x opened this issue Mar 4, 2019 · 2 comments
Closed

Possibly bug, TS wrongly complains that generic property is null #30205

al6x opened this issue Mar 4, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@al6x
Copy link

al6x commented Mar 4, 2019

TS v 3.3.3, enable strictNullChecks

Code

// Errors is a collection of form errors
//
// const errors = { 
//   title: ["can't be blank", "invalid characters"],
//   text:  ["too long"],
// }
//
// It could be rendered as
// 
// <FormInput name="title" errors={errors}/>


// Requiring that Errors had field of type T
type Errors<T extends string> = { [k in T]?: string[] }

export function FormInput<T extends string>(
  { name, errors }: { name: T, errors: Errors<T> }
) {
  // TS complains that there's no length
  const hasErrors = (errors[name] || []).length > 0
}

Expected behavior:

Should correctly infer that (errors[name] || []) is an Array.

Actual behavior:

Wrongly complains about null

Playground Link:

Play

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Mar 4, 2019
@RyanCavanaugh
Copy link
Member

Duplicate #22137 - we don't have type system machinery in place yet to narrow away undefined/null from types originating in a generic type.

@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants