microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.95k stars 12.47k forks source link

DOM suggestion: customisable `history.state` type #36178

Open OliverJAsh opened 4 years ago

OliverJAsh commented 4 years ago

Search Terms

dom lib window state history any unknown

Suggestion

Currently window.history.state has type any. Could we provide a way for users to customise this type? Perhaps by making it generic or allowing the type to be changed through declaration merging.

Failing that, could we replace the any type with unknown, so we have type safety and users are forced to validate/widen the type?

Another possible solution: if TypeScript provided a way to override any types (https://github.com/microsoft/TypeScript/issues/4062, https://github.com/microsoft/TypeScript/issues/26188).

Also related:

Use Cases

Examples

Checklist

My suggestion meets these guidelines:

falsandtru commented 4 years ago

Since history.state can be changed from anywhere, its type is unreliable. We have to validate history.state value each access. The value should be typed then.

falsandtru commented 4 years ago

Another code come from common libraries, other teams, or browser extensions may also use history.state.

https://github.com/defunkt/jquery-pjax/blob/master/jquery.pjax.js#L282-L293 https://github.com/falsandtru/pjax-api/blob/master/src/layer/data/store/state.ts

OliverJAsh commented 4 years ago

In that case it sounds like unknown is the best bet

nmain100 commented 4 years ago

If taking that approach, this becomes a duplicate of https://github.com/microsoft/TypeScript/issues/26188