You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Search Terms: typescript type guard object property properties not working
Code
classMyAudio{}letclips: {[path: string]: HTMLAudioElement|MyAudio}={};letpath='path/to/audio.ogg';if(clips[path]){if(clips[path]instanceofHTMLAudioElement){clips[path].currentTime=0;// This line produces error:// Property 'currentTime' does not exist on type 'MyAudio'.}}
Expected behavior:
TypeScript should realize that clips[path] is an instance of HTMLAudioElement and therefore currentTime is a perfectly valid property to access.
See also #31445 for some insight into why this can't work, but tl;dr: TS doesn't know clips[path] accesses the same property every time and therefore doesn't narrow it.
TypeScript Version: 3.5.1
Search Terms: typescript type guard object property properties not working
Code
Expected behavior:
TypeScript should realize that
clips[path]
is an instance ofHTMLAudioElement
and thereforecurrentTime
is a perfectly valid property to access.Actual behavior:
See error in example.
Playground Link: http://www.typescriptlang.org/play/#code/MYGwhgzhAECyCeBBArgEwJYHtoG8C+AUCAKYAu0o6ADhAFy7QDaVYpAFvRKQE7oB2AcwC69ABIAVWABkUGTAFESAW2J9yAHzhI0WaHmgBeXHgDcBImWgt2h6AHJrbAPSlMTsDswA6TAIF2zdAAzAAoKEGoIZlY2IWgASlwCaBToYLDKGmj2OP4uMD5gYkwg6AlpWSxFYhU1BKTUxvDI7NivYGRublVScXQVWwAGE2gnJ2hxNnQYCL5iK25MVGQimGIuzG5aZKaUsegABUWqddJ4ew6unr6VO2hUTGIYPkxyYgAPafJMPmgzk-sCEqmDsXh2KUIhCAA
The text was updated successfully, but these errors were encountered: